周一04.2流程控制if……else

时间:2023-03-09 19:51:25
周一04.2流程控制if……else

语法一: 

if 条件1:
代码1
代码2

例题:如果年龄>20岁,那么:叫阿姨

age=22
if age>20:
print('阿姨')

语法二:

if 条件1:
代码1
代码2
else:
代码1
代码2

例题:如果年龄>20岁,那么:叫阿姨,否则:叫姐姐

age=22
if age>20:
print('阿姨')
else:
print('姐姐')

语法三:

if 条件1:
代码1
代码2
if 条件2:
代码1
代码2
else:
else:
代码1
代码2

例题:如果年龄>=18并且<22岁 并且身高>170并且体重<100并且是漂亮的,那么:表白,否则:叫阿姨

age_of_girl=18
height=171
weight=99
is_pretty=True
success=False
if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True:
if success:
print('表白成功,在一起')
else:
print('...')
else:
print('阿姨好')

语法四:

if 条件1:
代码1
代码2
elif 条件2:
代码1
代码2
elif 条件3:
代码1
代码2
.......
else:
代码1
代码2

例题:如果成绩>=90,那么:优秀

如果成绩>=80且<90,那么:良好

如果成绩>=70且<80,那么:普通

其他情况:很差

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