鍍金池/ 教程/ Python/ exercise31.做出決定
附錄 A-練習(xí) 9:生成一個空文件(Touch, New-Item)
附錄 A-練習(xí) 10:復(fù)制文件 (cp)
exercise44.繼承 Vs.包含
附錄 A-練習(xí) 14:刪除文件 (rm)
附錄 A-練習(xí) 11:移動文件 (mv)
exercise46.項目骨架
附錄 A-練習(xí) 3:如果你迷路了
exercise37.復(fù)習(xí)符號
exercise47.自動化測試
exercise3.數(shù)字和數(shù)學(xué)計算
附錄 A-練習(xí) 1:安裝
exercise32.循環(huán)和列表
exercise31.做出決定
exercise42.對象、類、以及從屬關(guān)系
exercise48.更復(fù)雜的用戶輸入
下一步
簡介
附錄 A-練習(xí) 7:刪除路徑 (rmdir)
exercise49.寫代碼語句
exercise18.命名, 變量, 代碼, 函數(shù)
exercise12.提示別人
exercise14.提示和傳遞
exercise40.模塊, 類和對象
附錄 A-練習(xí) 12:查看文件 (less, MORE)
exercise9.打印, 打印, 打印
exercise13.參數(shù), 解包, 變量
exercise30. Else 和 If
exercise28. 布爾表達(dá)式
附錄 A-練習(xí) 4:創(chuàng)建一個路徑 (mkdir)
附錄 A-練習(xí) 15:退出命令行 (exit)
exercise25. 更多更多的練習(xí)
exercise6.字符串和文本
exercise2.注釋和井號“#”
exercise21. 函數(shù)的返回值
附錄 A-下一步
exercise1.第一個程序
exercise23. 閱讀代碼
附錄 A-練習(xí) 5:改變當(dāng)前路徑 (cd)
exercise17.更多文件操作
exercise24. 更多的練習(xí)
exercise19.函數(shù)和變量
exercise51.從瀏覽器獲取輸入
exercise22. 到目前為止你學(xué)到了什么?
exercise41.學(xué)會說面向?qū)ο?/span>
exercise52.開始你的 web 游戲
exercise20. 函數(shù)和文件
exercise15.讀文件
exercise45.你來制作一個游戲
exercise10.那是什么?
exercise8.打印, 打印
exercise35.分支和函數(shù)
exercise26. 恭喜你,可以進行一次考試了
exercise33.while 循環(huán)
exercise29. IF 語句
exercise36.設(shè)計和調(diào)試
exercise0.安裝和準(zhǔn)備
exercise50.你的第一個網(wǎng)站
附錄 A-練習(xí) 2:路徑, 文件夾, 名錄 (pwd)
exercise38.列表操作
附錄 A-練習(xí) 6:列出當(dāng)前路徑 (ls)
exercise16.讀寫文件
exercise4.變量和命名
exercise34.訪問列表元素
exercise11.提問
exercise43.基本的面向?qū)ο蟮姆治龊驮O(shè)計
附錄 A-簡介
附錄 A-練習(xí) 8:目錄切換(pushd, popd)
來自老程序員的建議
exercise27. 記住邏輯
exercise5.更多的變量和打印
exercise7.更多的打?。ㄝ敵觯?/span>
附錄 A-練習(xí) 13:輸出文件 (cat)
exercise39.字典,可愛的字典

exercise31.做出決定

這本書的上半部分你打印了一些東西,而且調(diào)用了函數(shù),不過一切都是直線式進行的。你的腳本從最上面一行開始,一路運行到結(jié)束,但其中并沒有決定程序流向的分支點?,F(xiàn)在你已經(jīng)學(xué)了 if,else,和 elif,你就可以開始創(chuàng)建包含條件判斷的腳本了。

上一個腳本中你寫了一系列的簡單提問測試。這節(jié)的腳本中,你將需要向用戶提問,依據(jù)用戶的答案來做出決定。把腳本寫下來,多多鼓搗一陣子,看看它的工作原理是什么。

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 "You 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 "You stumble around and fall on a knife and die.  Good job!"

這里的重點是你可以在“if 語句”內(nèi)部再放一個“if 語句”。這是一個很強大的功能,可以用來創(chuàng)建嵌套(nested),其中的一個分支將引向另一個分支的子分支。

你需要理解 if 語句包含 if 語句的概念。做一下附加題,確保自己真正理解了它們。

你看到的結(jié)果

下面是我玩這個小游戲的結(jié)果,我玩的不怎么樣:

$ python ex31.py
You enter a dark room with two doors.  Do you go through door #1 or door #2?
>  1
There's a giant bear here eating a cheese cake.  What do you do?
1. Take the cake.
2. Scream at the bear.
>  2
The bear eats your legs off.  Good job!

附加題

1.為游戲添加新的部分,改變玩家做決定的位置。盡自己的能力擴展這個游戲,不過別把游戲弄得太怪異了。 2.寫一個全新的游戲,你可能不喜歡我提供的這個,那么自己寫一個玩玩。這是你的電腦,你可以用它做任何自己想做的事情。

常見問題

Q: 可以用 if-else 替換 elif 嗎?

某些情況下可以, 但是這個也依賴于每一個 if/else 是怎么寫的 。這也意味著, Python 會檢查每個 if-else 的組合,而不是只檢查 if-elif-else 組合中的第一個為假的分支,嘗試用兩種方式多編寫一些代碼,以找出他們的不同點。

Q:我怎么知道一個數(shù)字是在一個數(shù)字范圍之間?

有兩種方法: 一種經(jīng)典的方式是使用 0 < x < 10 或者 1 <= x < 10,另一中方式是使用 x in range(1, 10)。

Q: 怎樣才能在 if-elif-else 代碼塊中增加更多的選擇?

為每一個可能的選擇增加一個 elif 代碼塊。