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