笨办法学Python3 习题30 else 和 if

 1 people = 30
 2 cars = 40
 3 trucks = 15
 4 
 5 if cars > people:                     # 下面同时为 True, 也只会运行第一个为 True 的块 ,另外两个优先级依次低于if 
 6     print("We should take the cars.") # 第一个分支的块
 7 
 8 elif cars < people:                   # 第二个分支
 9     print("We should not take the cars.")
10 
11 else :                                # 第三个分支
12     print("We can't decide.")
13 
14 
15 
16 if trucks > cars:
17     print("That's too many trucks.")
18 
19 elif trucks < cars:
20     print("Maybe we could take the trucks.")
21 
22 else:
23     print("We still can't decide.")
24 
25 
26 
27 if people > trucks:
28     print("Allright,let's just take the trucks.")
29 
30 else:
31     print("Fine,let's stay home then.")

 

PS C:\Users\Administrator\lpthw> python ex30.py
We should take the cars.
Maybe we could take the trucks.
Allright,let's just take the trucks.

 

posted @ 2023-10-09 12:58  萹豆  阅读(68)  评论(0编辑  收藏  举报