Analyzing Process Control --- IF

if the judge

if: life judge, what if conditions are met, to do something.

Instructions:

if 条件 :
    事情  #后面同意缩进内,都是满足条件后,需要做的事情
    ……

if a single branch structure ---> if

Suppose there are two variables a and b, a same assignment, if used to determine the values ​​of variables a and b are equal, if they are equal, then the print ture;

# # 单分支结构

a = b = 1               #把1分别给变量a和b赋值

if a == b:              #判断a和b的值是否相等
    print('true')       

The result:

true

Process finished with exit code 0

if two-branch structure ---> if ... else

In general, the determination of the results are at least two, such determination 1等于2result is not correct, is wrong.

if ... else said: what to do when back if conditions are met, otherwise what do

Also assume that there are two variables a and b, 1 and 2 were assigned, determines the value of variables a and b are equal, if want to wait, the printing ture; if not equal, then the print false.

# # 双分支结构

a = 1
b = 2
if a == b:
    print('true')
else:
    print('false')

The result:

false

Process finished with exit code 0

The values ​​of the variables a and b are interchangeable

# # 双分支结构

a = 2
b = 1
if a == b:
    print('true')
else:
    print('false')

The result:

false

Process finished with exit code 0

if the multi-branch structure ---> if ... elif ... else

if ... elif ... else, he said: if conditions after the establishment of what to do, or else determine what conditions the establishment elif doing, or what else to do ... intermediate conditions can always increase

Define a variable a, '' breakfast '', '' lunch '', '' dinner '' when observing the printed result when each assignment:

# # 多分支结构

a = '早餐'
# a = '午餐'
# a = '晚餐'

if   a == '早餐':
    print('豆浆')
elif a == '午餐':
    print('米饭')
elif a == '晚餐':
    print('面条')

After running show:

豆浆

Process finished with exit code 0
米饭

Process finished with exit code 0
面条

Process finished with exit code 0

Exercise

  • If the score> = 90 print "excellent"
  • If the score> = 80 and score <90, print "good"
  • If the score> = 70 and score <80, print "normal"
  • Other cases: print "poor"

Implementation code:

score = 60

if score >= 90:
    print('优秀')
elif score >=80:
    print('良好')
elif score >=70:
    print('普通')
else:
    print('差')

Guess you like

Origin www.cnblogs.com/liveact/p/11425937.html