目录

* 语法(掌握) <https://www.cnblogs.com/nickchen121/p/10737326.html#语法掌握>
* if <https://www.cnblogs.com/nickchen121/p/10737326.html#if>
* if...else <https://www.cnblogs.com/nickchen121/p/10737326.html#if...else>
* if...elif...else
<https://www.cnblogs.com/nickchen121/p/10737326.html#if...elif...else>
* 练习(掌握) <https://www.cnblogs.com/nickchen121/p/10737326.html#练习掌握>
* 练习1:成绩评判 <https://www.cnblogs.com/nickchen121/p/10737326.html#练习1成绩评判>
* 练习2:模拟登录注册 <https://www.cnblogs.com/nickchen121/p/10737326.html#练习2模拟登录注册>
* if的嵌套(掌握) <https://www.cnblogs.com/nickchen121/p/10737326.html#if的嵌套掌握>
语法(掌握)

if判断是干什么的呢?if判断其实是在模拟人做判断。就是说如果这样干什么,如果那样干什么。对于ATM系统而言,则需要判断你的账号密码的正确性。

if


学什么都是为了让计算机向人一样工作,我们无时无刻都在判断。路边路过一个生物,你会判断两个人是不是会表白?首先会判断这个生物是不是人类,并且这个人类是个女人,年龄大于18小于20几岁。你首先需要记录一堆数据,然后才会用你的大脑去判断。if表示if成立代码成立会干什么。
if 条件: 代码1 代码2 代码3 ... #
代码块(同一缩进级别的代码,例如代码1、代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行)

# if cls = 'human' gender = 'female' age = 18 if cls == 'human' and gender ==
'female' and age > 16 and age < 22: print('开始表白') print('end...') 开始表白 end...
if...else
if 条件: 代码1 代码2 代码3 ... else: 代码1 代码2 代码3 ...
if...else表示if成立代码成立会干什么,else不成立会干什么。
# if...else cls = 'human' gender = 'female' age = 38 if cls == 'human' and
gender == 'female' and age > 16 and age < 22: print('开始表白') else: print('阿姨好')
阿姨好


if...elif...else
if 条件1: 代码1 代码2 代码3 ... elif 条件2: 代码1 代码2 代码3 ... elif 条件3: 代码1 代码2 代码3 ...
... else: 代码1 代码2 代码3 ...
if...elif...else表示if条件1成立干什么,elif条件2成立干什么,elif条件3成立干什么,elif...否则干什么。
# if...elif...else cls = 'human' gender = 'female' age = 28 if cls == 'human'
and gender == 'female' and age > 16 and age < 22: print('开始表白') elif cls ==
'human' and gender == 'female' and age > 22 and age < 30: print('考虑下') else:
print('阿姨好') 考虑下
练习(掌握)

练习1:成绩评判

* 如果 成绩>=90,打印"优秀"
* 如果 成绩>=80 并且 成绩<90,打印"良好"
* 如果 成绩>=70 并且 成绩<80,打印"普通"
* 其他情况:打印"差" # 成绩评判 score = input("your score: ") score = int(score) if score
>= 90: print('优秀') # elif score >= 80 and score < 90: elif score >= 80:
print('良好') # elif score >= 70 and score < 80: elif score >= 70: print('普通')
else: print('差') your score: 80 良好
练习2:模拟登录注册
# 模拟登录注册 user_from_db = 'nick' pwd_from_db = '123' user_from_inp =
input('username: ') user_from_inp = input('password: ') if user_from_inp ==
user_from_db and pwd_from_inp == pwd_from_db: print('login successful') else:
print('username or password error') username: nick password: 123 username or
password error


if的嵌套(掌握)

如果我们表白的时候,表白成功的时候我们是不是会做什么,表白不成功是不是又会会做什么呢?
# if的嵌套 cls = 'human' gender = 'female' age = 18 is_success = False if cls ==
'human' and gender == 'female' and age > 16 and age < 22: print('开始表白') if
is_success: print('那我们一起走吧...') else: print('我逗你玩呢') else: print('阿姨好') 开始表白
我逗你玩呢

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信