练习30--else和if

Posted luoxun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习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_work4_python2_code_python2_LearnPythonTheHardWay> python ex30.py
We should take the cars.
Maybe we could take the trucks.
Alright,lets just take the trucks.

一些问题:

1. 试着猜猜 elif 和 else 的作用是什么。
在if的条件不满足的情况下进行下一步判断,并执行另外的操作

以上是关于练习30--else和if的主要内容,如果未能解决你的问题,请参考以下文章