本節課是一小段類似《龍與地下城》的遊戲的**,是if語句巢狀的深入,即巢狀的if語句中又出現巢狀的if語句。理論上可以巢狀許多層,至於上限是多少,暫不清楚。
原**如下:
print
"you enter a dark room with two doors. do you go through door #1 or door #2?"
door = raw_input("> ")
if door == "1":
print
"there's a giant bear here eating a cheese cake. what do you do?"
print
"1. take the cake."
print
"2. scream at the bear."
bear = raw_input("> ")
if bear == "1":
print
"the bear eats your face off. good job!"
elif bear== "2":
print
"the bear eats your legs off. good job!"
else:
print
"well,doing %s is probably better. bear runs away." % bear
elif door == "2":
print
"your stare into the endless abyss at cthulhu's retina."
print
"1. blueberries."
print
"2. yellow jacket clothespins."
print
"3. understanding revolvers yelling melodies."
insanity = raw_input("> ")
if insanity == "1"
or insanity =="2":
print
"your body survives powered by a mind of jello. good job!"
else:
print
"the insanity rots your eyes into a pool of muck. good job!"
else:
print
"your stumble around and fall on a knife and die. good job!"
當使用者輸入的數字是1,2時結果如下(注意這裡的1和2都是字元型資料,不是數值):
根據這段**的思路,自上而下先列乙個選擇分支草圖,並在草圖上畫出各個動作,當你覺得分支足夠多時,選擇其中乙個分支作為找到寶藏遊戲勝利,其餘分支均為死亡遊戲結束。這樣你就寫出了乙個自己的《龍與地下城》遊戲。
仔細閱讀常見問題解答,裡面有判斷乙個數值是否屬於某個範圍的語法。
《笨辦法學Python》 第4課手記
這節課目的是讓你掌握變數,跟c語言非常類似,很簡單。左邊是變數名用 號給變數賦值。不同的是我沒有看到變數宣告,作者是直接賦值,拿過來就用的。至於接下來作者舉的報錯那個例子,如果你把變數名打錯的情況下就會出現,因為變數名一旦打錯,python將不能識別,也就無法在print中輸出對應的值。這裡我們可以...
《笨辦法學Python》 第6課手記
第6課講字串和文字,作者給出的 如下 x there are d types of people.10 binary binary do not don t y those who know s and those who s.binary,do not print xprint yprint i ...
《笨辦法學Python》 第8課手記
第八課沒有新內容,作者在常見問題解答裡面說得很清楚,建議每一課的常見問題解答都要仔細閱讀。如下 formatter r r r r print formatter 1,2,3,4 print formatter one two three four print formatter formatter...