前一習題中你寫了一些 “if 語句(if-statements)”,并且試圖猜出它們是什么,以及實現(xiàn)的是什么功能。在你繼續(xù)學習之前,我給你解釋一下上一節(jié)的加分習題的答案。上一節(jié)的加分習題你做過了吧,有沒有?
把我的答案和你的答案比較一下,確認自己真正懂得代碼“區(qū)段”的含義。這點對于你下一節(jié)的練習很重要,因為你將會寫很多的 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.
$