笨方法”学Python3,习题 30:else 和 if
1、 if else elif 怎么用 ?
一、基础代码
所写的代码:
people = 20
cars = 40
trucks = 15
if cars > people:
print("We should take the cars.")
elif cars < people:
print("We should not take the cars.")
else:
print("We can't decide.")
if trucks > cars:
print("That's too many trucks.")
elif trucks < cars:
print("Maybe we could take the trucks.")
else:
print("We still can't decide.")
if people > trucks:
print("Alright, let's just take the trucks.")
else:
print("Fine, let's stay home then.")
---------------------------------------------------------------------------------------------------
应该看到的结果:
PS D:\代码> & D:/软件/python/python.exe d:/代码/ex30.py
We should take the cars.
Maybe we could take the trucks.
Alright, let's just take the trucks.
二、巩固练习1、猜想一下 elif 和 else 的功能if :如果...就...
elif : 再如果...就...
else : 否则...
2、将变量的数改掉,观察结果
4、添加备注
总结:
1、if 条件语句可以用 elif 和 else 来追加条件
2、if 如果... elif 再如果... else 否则...
^ v ^,知乎此系列文章内容均会在微信公众号中同步更新,公众号:小民有个小旮旯