前一习题中你写了一些 “if 语句(if-statements)”,并且试图猜出它们是什么,以及实现的是什么功能。在你继续学习之前,我给你解释一下上一节的加分习题的答案。上一节的加分习题你做过了吧,有没有?
把我的答案和你的答案比较一下,确认自己真正懂得代码“区段”的含义。这点对于你下一节的练习很重要,因为你将会写很多的 if 语句。
把这一段写下来,并让它运行起来:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | people = 30
cars = 40
buses = 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 buses > cars:
print "That's too many buses."
elif buses < cars:
print "Maybe we could take the buses."
else:
print "We still can't decide."
if people > buses:
print "Alright, let's just take the buses."
else:
print "Fine, let's stay home then."
|
$ python ex30.py
We should take the cars.
Maybe we could take the buses.
Alright, let's just take the buses.
$