在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

exercise31.做出決定

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

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

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!"

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

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

你看到的結(jié)果

下面是我玩這個(gè)小游戲的結(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.為游戲添加新的部分,改變玩家做決定的位置。盡自己的能力擴(kuò)展這個(gè)游戲,不過(guò)別把游戲弄得太怪異了。 2.寫一個(gè)全新的游戲,你可能不喜歡我提供的這個(gè),那么自己寫一個(gè)玩玩。這是你的電腦,你可以用它做任何自己想做的事情。

常見(jiàn)問(wèn)題

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

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

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

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

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

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