<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 練習31.做出決定 這本書的上半部分你打印了一些東西,而且調用了函數,不過一切都是直線式進行的。你的腳本從最上面一行開始,一路運行到結束,但其中并沒有決定程序流向的分支點。現在你已經學了`if`,`else`,和`elif`,你就可以開始創建包含條件判斷的腳本了。 上一個腳本中你寫了一系列的簡單提問測試。這節的腳本中,你將需要向用戶提問,依據用戶的答案來做出決定。把腳本寫下來,多多鼓搗一陣子,看看它的工作原理是什么。 ~~~ 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語句”內部再放一個“if語句”。這是一個很強大的功能,可以用來創建嵌套(nested),其中的一個分支將引向另一個分支的子分支。 你需要理解`if語句`包含`if語句`的概念。做一下附加題,確保自己真正理解了它們。 ## 你看到的結果 下面是我玩這個小游戲的結果,我玩的不怎么樣: ~~~ $ 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. 為游戲添加新的部分,改變玩家做決定的位置。盡自己的能力擴展這個游戲,不過別把游戲弄得太怪異了。 > 1. 寫一個全新的游戲,你可能不喜歡我提供的這個,那么自己寫一個玩玩。這是你的電腦,你可以用它做任何自己想做的事情。 ## 常見問題 ### Q: 可以用`if-else`替換`elif` 嗎? > 某些情況下可以, 但是這個也依賴于每一個`if/else`是怎么寫的 。這也意味著, Python會檢查每個if-else的組合,而不是只檢查`if-elif-else`組合中的第一個為假的分支,嘗試用兩種方式多編寫一些代碼,以找出他們的不同點。 ### Q:我怎么知道一個數字是在一個數字范圍之間? > 有兩種方法: 一種經典的方式是使用`0 < x < 10` 或者 `1 <= x < 10`,另一中方式是使用`x in range(1, 10)`。 ### Q: 怎樣才能在`if-elif-else`代碼塊中增加更多的選擇? > 為每一個可能的選擇增加一個`elif` 代碼塊。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看