首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >if 条件判断

if 条件判断

作者头像
py3study
发布2020-01-19 11:13:51
发布2020-01-19 11:13:51
2.5K0
举报
文章被收录于专栏:python3python3

条件语句的执行过程:

if 条件判断注意: 1.每个条件后面要使用冒号 : ,表示条件为True时要执行的代码; 2.使用缩进来划分代码块,相同缩进数的语句在一起组成一个代码块。

if...else,单条件判断

代码语言:javascript
复制
 1 username_store = 'lipandeng'
 2 password_store = '123'
 3 
 4 username_input = input('your username:')
 5 password_input = input('your password:')
 6 
 7 if username_input == username_store and password_input == password_input:
 8     print('welcome {0}'.format(username_input))
 9 else:
10     print('Invalid username and password!')

if...elif...else,多条件判断

代码语言:javascript
复制
 1 score = int(input('Enter your score:'))     # input()返回的数据类型是str,int()函数是把str转int。
 2 
 3 if score < 0 or score > 100:    # 条件1,此条件为True时执行print(),elif后面的代码不再执行。
 4     print('Scores of the range is 0-100.')
 5 elif score >= 90:   # 条件2,当条件1为False时判断条件2,此条件为True时执行print(),elif后面的代码不再执行。
 6     print('Your score is excellent.')
 7 elif score >= 60:   # 条件3,当条件1和条件2为False时判断条件3,此条件为True时后执行print(),elif后面的代码不再执行。
 8     print('Your score is good.')
 9 else:   # 条件4,以上判断条件都为False时执行的print()。
10     print('Your score is not pass the exam.')
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/03/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档