1 单分支判断
In [6]:
age = 17
if age >= 18:
print('你已经成年')
print('可以去上网')
print('赶紧回家吃饭')
赶紧回家吃饭
2 双分支判断
In [7]:
age = 17
if age >=18:
print('你已经成年,可以去上网')
else:
print('赶紧回家吃饭,快快长大')
赶紧回家吃饭,快快长大
3 练习
In [1]:
score = 59
if score >= 90 and score <= 100:
print('优秀')
if score >= 80 and score <= 90:
print('良好')
if score >= 60 and score <= 80:
print('及格')
if score >= 0 and score <= 60:
print('不及格')
不及格
In [2]:
score = 59
if 90 <= score <= 100:
print('优秀')
if 80 <= score <= 90:
print('良好')
if 60 <= score <= 80:
print('及格')
if 0 <= score <= 60:
print('不及格')
不及格
3 if嵌套
因为上面代码每一个if都会执行,为了节约性能使用多分支判断,当有一个if执行时,后面的if不会被执行
In [3]:
score = 59
if 90 <= score <= 100:
print('优秀')
else:
if 80 <= score <= 90:
print('良好')
else:
if 60 <= score <= 80:
print('及格')
else:
if 0 <= score <= 60:
print('不及格')
不及格
4 多分支判断
In [4]:
score = 59
if 90 <= score <= 100:
print('优秀')
elif 80 <= score <= 90:
print('良好')
elif 60 <= score <= 80:
print('及格')
elif 0 <= score <= 60:
print('不及格')
不及格