首页 > 其他 > 详细

周一04.2流程控制if……else

时间:2018-12-29 20:32:32      阅读:136      评论:0      收藏:0      [点我收藏+]

语法一: 

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(‘很差‘)


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

周一04.2流程控制if……else

原文:https://www.cnblogs.com/wanglimei/p/10197684.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!