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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 習題 41: 來自 Percal 25 號行星的哥頓人(Gothons) 你在上一節中發現 dict 的秘密功能了嗎?你可以解釋給自己嗎?讓我來給你解釋一下,順便和你自己的理解對比看有什么不同。這里是我們要討論的代碼: ~~~ cities['_find'] = find_city city_found = cities['_find'](cities, state) ~~~ 你要記住一個函數也可以作為一個變量,``def find_city`` 比如這一句創建了一個你可以在任何地方都能使用的變量。在這段代碼里,我們首先把函數 find_city 放到叫做 cities 的字典中,并將其標記為 '_find'。 這和我們將州和市關聯起來的代碼做的事情一樣,只不過我們在這里放了一個函數的名稱。 好了,所以一旦我們知道 find_city 是在字典中 _find 的位置,這就意味著我們可以去調用它。第二行代碼可以分解成如下步驟: 1. Python 看到 city_found= 于是知道了需要創建一個變量。 1. 然后它讀到 cities ,然后知道了它是一個字典 1. 然后看到了 ['_find'] ,于是 Python 就從索引找到了字典 cities 中對應的位置,并且獲取了該位置的內容。 1. ['_find'] 這個位置的內容是我們的函數 find_city ,所以 Python 就知道了這里表示一個函數,于是當它碰到 ( 就開始了函數調用。 1. cities,state 這兩個參數將被傳遞到函數 find_city 中,然后這個函數就被運行了。 1. find_city 接著從 cities 中尋找 states ,并且返回它找到的內容,如果什么都沒找到,就返回一個信息說它什么都沒找到。 1. Python find_city 接受返回的信息,最后將該信息賦值給一開始的 city_found 這個變量。 我再教你一個小技巧。如果你倒著閱讀的話,代碼可能會變得更容易理解。讓我們來試一下,一樣是那行: 1. state 和 city 是... 1. 作為參數傳遞給... 1. 一個函數,位置在... 1. '_find' 然后尋找,目的地為... 1. cities 這個位置... 1. 最后賦值給 city_found. 還有一種方法讀它,這回是“由里向外”。 1. 找到表達式的中心位置,此次為 ['_find']. 1. 逆時針追溯,首先看到的是一個叫 cities 的字典,這樣就知道了 cities 中的 _find 元素。 1. 上一步得到一個函數。繼續逆時針尋找,看到的是參數。 1. 參數傳遞給函數后,函數會返回一個值。然后再逆時針尋找。 1. 最后,我們到了 city_found= 的賦值位置,并且得到了最終結果。 數十年的編程下來,我在讀代碼的過程中已經用不到上面的三種方法了。我只要瞟一眼就能知道它的意思。甚至給我一整頁的代碼,我也可以一眼瞄出里邊的 bug 和錯誤。這樣的技能是花了超乎常人的時間和精力才鍛煉得來的。在磨練的過程中,我學會了下面三種讀代碼的方法,它們適用于幾乎所有的編程語言: 1. 從前向后。 1. 從后向前。 1. 逆時針方向。 下次碰到難懂的語句時,你可以試試這三種方法。 現在我們來寫這次的練習,寫完后再過一遍,這節習題其實挺有趣的。 <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&#13; 77&#13; 78&#13; 79&#13; 80&#13; 81</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>import random&#13; from urllib import urlopen&#13; import sys&#13; &#13; WORD_URL = "http://learncodethehardway.org/words.txt"&#13; WORDS = []&#13; &#13; PHRASES = {&#13; "class ###(###):":&#13; "Make a class named ### that is-a ###.",&#13; "class ###(object):\n\tdef __init__(self, ***)" :&#13; "class ### has-a __init__ that takes self and *** parameters.",&#13; "class ###(object):\n\tdef ***(self, @@@)":&#13; "class ### has-a function named *** that takes self and @@@ parameters.",&#13; "*** = ###()":&#13; "Set *** to an instance of class ###.",&#13; "***.***(@@@)":&#13; "From *** get the *** function, and call it with parameters self, @@@.",&#13; "***.*** = '***'":&#13; "From *** get the *** attribute and set it to '***'."&#13; }&#13; &#13; # do they want to drill phrases first&#13; PHRASE_FIRST = False&#13; if len(sys.argv) == 2 and sys.argv[1] == "english":&#13; PHRASE_FIRST = True&#13; &#13; # load up the words from the website&#13; for word in urlopen(WORD_URL).readlines():&#13; WORDS.append(word.strip())&#13; &#13; &#13; def convert(snippet, phrase):&#13; class_names = [w.capitalize() for w in&#13; random.sample(WORDS, snippet.count("###"))]&#13; other_names = random.sample(WORDS, snippet.count("***"))&#13; results = []&#13; param_names = []&#13; &#13; for i in range(0, snippet.count("@@@")):&#13; param_count = random.randint(1,3)&#13; param_names.append(', '.join(random.sample(WORDS, param_count)))&#13; &#13; for sentence in snippet, phrase:&#13; result = sentence[:]&#13; &#13; # fake class names&#13; for word in class_names:&#13; result = result.replace("###", word, 1)&#13; &#13; # fake other names&#13; for word in other_names:&#13; result = result.replace("***", word, 1)&#13; &#13; # fake parameter lists&#13; for word in param_names:&#13; result = result.replace("@@@", word, 1)&#13; &#13; results.append(result)&#13; &#13; return results&#13; &#13; &#13; # keep going until they hit CTRL-D&#13; try:&#13; while True:&#13; snippets = PHRASES.keys()&#13; random.shuffle(snippets)&#13; &#13; for snippet in snippets:&#13; phrase = PHRASES[snippet]&#13; question, answer = convert(snippet, phrase)&#13; if PHRASE_FIRST:&#13; question, answer = answer, question&#13; &#13; print question&#13; &#13; raw_input("&gt; ")&#13; print "ANSWER: %s\n\n" % answer&#13; except EOFError:&#13; print "\nBye"&#13; </pre> </div> </td> </tr></tbody></table> 代碼不少,不過還是從頭寫完吧。確認它能運行,然后玩一下看看。 ### 你應該看到的結果 我玩起來時這樣的: ~~~ $ python ex41.py bat.bait(children) > From bat get the bait function and call it with self and children arguments. ANSWER: From bat get the bait function, and call it with parameters self, children. class Brake(object): def __init__(self, beef) > class Brake has a __init__ function that takes self and beef parameters. ANSWER: class Brake has-a __init__ that takes self and beef parameters. class Cow(object): def crook(self, cushion) > class Cow has-a function named crook that takes self and cushion params. ANSWER: class Cow has-a function named crook that takes self and cushion parameters. cast = Beetle() > Set cast to an instance of class Beetle. ANSWER: Set cast to an instance of class Beetle. cent.coach = 'appliance' > From cent get the coach attribute and set it to appliance. ANSWER: From cent get the coach attribute and set it to 'appliance'. class Destruction(Committee): > ^D Bye ~~~ ### 加分習題 1. 解釋一下返回至下一個房間的工作原理。 1. 創建更多的房間,讓游戲規模變大。 1. 除了讓每個函數打印自己以外,再學習一下“文檔字符串(doc strings)”式的注解。看看你能不能將房間描述寫成文檔注解,然后修改運行它的代碼,讓它把文檔注解打印出來。 1. 一旦你用了文檔注解作為房間描述,你還需要讓這個函數打印出用戶提示嗎?試著讓運行函數的代碼打出用戶提示來,然后將用戶輸入傳遞到各個函數。你的函數應該只是一些 if 語句組合,將結果打印出來,并且返回下一個房間。 1. 這其實是一個小版本的“有限狀態機(finite state machine)”,找資料閱讀了解一下,雖然你可能看不懂,但還是找來看看吧。 1. 我的代碼里有一個 bug,為什么門鎖要猜測 11 次?
                  <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>

                              哎呀哎呀视频在线观看