练习30--else和if

一 代码及执行结果

ex30.py

 1 people = 14
 2 cars = 15
 3 trucks = 10
 4 
 5 if cars > people:
 6     print("We should take the cars.")
 7 elif cars < people:
 8     print("We should not take the cars.")
 9 else:
10     print("We can't decide.")
11 
12 if trucks > cars:
13     print("That too many trucks.")
14 elif trucks < cars:
15     print("Maybe we could take the trucks.")
16 else :
17     print("We stil can't decide.")
18 
19 if people > trucks:
20     print("Alright,let's just take the trucks.")
21 else:
22     print("Fine, let's stay home then.")

执行结果:

PS E:\3_work\4_python\2_code_python\02_LearnPythonTheHardWay> python ex30.py
We should take the cars.
Maybe we could take the trucks.
Alright,let's just take the trucks.

一些问题:

1. 试着猜猜 elif 和 else 的作用是什么。
在if的条件不满足的情况下进行下一步判断,并执行另外的操作
posted @ 2020-07-13 12:23  洛兰123  阅读(164)  评论(0编辑  收藏  举报