<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 習題 35: 分支和函數 你已經學會了 if語句、函數、還有列表。現在你要練習扭轉一下思維了。把下面的代碼寫下來,看你是否能弄懂它實現的是什么功能。 <table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 1&#13; 2&#13; 3&#13; 4&#13; 5&#13; 6&#13; 7&#13; 8&#13; 9&#13; 10&#13; 11&#13; 12&#13; 13&#13; 14&#13; 15&#13; 16&#13; 17&#13; 18&#13; 19&#13; 20&#13; 21&#13; 22&#13; 23&#13; 24&#13; 25&#13; 26&#13; 27&#13; 28&#13; 29&#13; 30&#13; 31&#13; 32&#13; 33&#13; 34&#13; 35&#13; 36&#13; 37&#13; 38&#13; 39&#13; 40&#13; 41&#13; 42&#13; 43&#13; 44&#13; 45&#13; 46&#13; 47&#13; 48&#13; 49&#13; 50&#13; 51&#13; 52&#13; 53&#13; 54&#13; 55&#13; 56&#13; 57&#13; 58&#13; 59&#13; 60&#13; 61&#13; 62&#13; 63&#13; 64&#13; 65&#13; 66&#13; 67&#13; 68&#13; 69&#13; 70&#13; 71&#13; 72&#13; 73&#13; 74&#13; 75&#13; 76</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>from sys import exit&#13; &#13; def gold_room():&#13; print "This room is full of gold. How much do you take?"&#13; &#13; next = raw_input("&gt; ")&#13; if "0" in next or "1" in next:&#13; how_much = int(next)&#13; else:&#13; dead("Man, learn to type a number.")&#13; &#13; if how_much &lt; 50:&#13; print "Nice, you're not greedy, you win!"&#13; exit(0)&#13; else:&#13; dead("You greedy bastard!")&#13; &#13; &#13; def bear_room():&#13; print "There is a bear here."&#13; print "The bear has a bunch of honey."&#13; print "The fat bear is in front of another door."&#13; print "How are you going to move the bear?"&#13; bear_moved = False&#13; &#13; while True:&#13; next = raw_input("&gt; ")&#13; &#13; if next == "take honey":&#13; dead("The bear looks at you then slaps your face off.")&#13; elif next == "taunt bear" and not bear_moved:&#13; print "The bear has moved from the door. You can go through it now."&#13; bear_moved = True&#13; elif next == "taunt bear" and bear_moved:&#13; dead("The bear gets pissed off and chews your leg off.")&#13; elif next == "open door" and bear_moved:&#13; gold_room()&#13; else:&#13; print "I got no idea what that means."&#13; &#13; &#13; def cthulhu_room():&#13; print "Here you see the great evil Cthulhu."&#13; print "He, it, whatever stares at you and you go insane."&#13; print "Do you flee for your life or eat your head?"&#13; &#13; next = raw_input("&gt; ")&#13; &#13; if "flee" in next:&#13; start()&#13; elif "head" in next:&#13; dead("Well that was tasty!")&#13; else:&#13; cthulhu_room()&#13; &#13; &#13; def dead(why):&#13; print why, "Good job!"&#13; exit(0)&#13; &#13; def start():&#13; print "You are in a dark room."&#13; print "There is a door to your right and left."&#13; print "Which one do you take?"&#13; &#13; next = raw_input("&gt; ")&#13; &#13; if next == "left":&#13; bear_room()&#13; elif next == "right":&#13; cthulhu_room()&#13; else:&#13; dead("You stumble around the room until you starve.")&#13; &#13; &#13; start()&#13; </pre> </div> </td> </tr></tbody></table> ### 你應該看到的結果 下面是我玩游戲的過程: ~~~ $ python ex35.py You are in a dark room. There is a door to your right and left. Which one do you take? > left There is a bear here. The bear has a bunch of honey. The fat bear is in front of another door. How are you going to move the bear? > taunt bear The bear has moved from the door. You can go through it now. > open door This room is full of gold. How much do you take? > asf Man, learn to type a number. Good job! $ ~~~ ### 加分習題 1. 把這個游戲的地圖畫出來,把自己的路線也畫出來。 1. 改正你所有的錯誤,包括拼寫錯誤。 1. 為你不懂的函數寫注解。記得文檔注解該怎么寫嗎? 1. 為游戲添加更多元素。通過怎樣的方式可以簡化并且擴展游戲的功能呢? 1. 這個 gold_room 游戲使用了奇怪的方式讓你鍵入一個數字。這種方式會導致什么樣的 bug? 你可以用比檢查 0、1 更好的方式判斷輸入是否是數字嗎?int() 這個函數可以給你一些頭緒。
                  <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>

                              哎呀哎呀视频在线观看