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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 習題 25: 更多更多的練習 我們將做一些關于函數和變量的練習,以確認你真正掌握了這些知識。這節練習對你來說可以說是一本道:寫程序,逐行研究,弄懂它。 不過這節練習還是有些不同,你不需要運行它,取而代之,你需要將它導入到 python 里通過自己執行函數的方式運行。 <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</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>def break_words(stuff):&#13; """This function will break up words for us."""&#13; words = stuff.split(' ')&#13; return words&#13; &#13; def sort_words(words):&#13; """Sorts the words."""&#13; return sorted(words)&#13; &#13; def print_first_word(words):&#13; """Prints the first word after popping it off."""&#13; word = words.pop(0)&#13; print word&#13; &#13; def print_last_word(words):&#13; """Prints the last word after popping it off."""&#13; word = words.pop(-1)&#13; print word&#13; &#13; def sort_sentence(sentence):&#13; """Takes in a full sentence and returns the sorted words."""&#13; words = break_words(sentence)&#13; return sort_words(words)&#13; &#13; def print_first_and_last(sentence):&#13; """Prints the first and last words of the sentence."""&#13; words = break_words(sentence)&#13; print_first_word(words)&#13; print_last_word(words)&#13; &#13; def print_first_and_last_sorted(sentence):&#13; """Sorts the words then prints the first and last one."""&#13; words = sort_sentence(sentence)&#13; print_first_word(words)&#13; print_last_word(words)&#13; </pre> </div> </td> </tr></tbody></table> 首先以正常的方式 pythonex25.py 運行,找出里邊的錯誤,并把它們都改正過來。然后你需要接著下面的答案章節完成這節練習。 ### 你應該看到的結果 這節練習我們將在你之前用來做算術的 python 編譯器里,用交互的方式和你的.py 作交流。 這是我做出來的樣子: <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</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>$ python&#13; Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) &#13; [GCC 4.0.1 (Apple Inc. build 5465)] on darwin&#13; Type "help", "copyright", "credits" or "license" for more information.&#13; &gt;&gt;&gt; import ex25&#13; &gt;&gt;&gt; sentence = "All good things come to those who wait."&#13; &gt;&gt;&gt; words = ex25.break_words(sentence)&#13; &gt;&gt;&gt; words&#13; ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.']&#13; &gt;&gt;&gt; sorted_words = ex25.sort_words(words)&#13; &gt;&gt;&gt; sorted_words&#13; ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']&#13; &gt;&gt;&gt; ex25.print_first_word(words)&#13; All&#13; &gt;&gt;&gt; ex25.print_last_word(words)&#13; wait.&#13; &gt;&gt;&gt; wrods&#13; Traceback (most recent call last):&#13; File "&lt;stdin&gt;", line 1, in &lt;module&gt;&#13; NameError: name 'wrods' is not defined&#13; &gt;&gt;&gt; words&#13; ['good', 'things', 'come', 'to', 'those', 'who']&#13; &gt;&gt;&gt; ex25.print_first_word(sorted_words)&#13; All&#13; &gt;&gt;&gt; ex25.print_last_word(sorted_words)&#13; who&#13; &gt;&gt;&gt; sorted_words&#13; ['come', 'good', 'things', 'those', 'to', 'wait.']&#13; &gt;&gt;&gt; sorted_words = ex25.sort_sentence(sentence)&#13; &gt;&gt;&gt; sorted_words&#13; ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who']&#13; &gt;&gt;&gt; ex25.print_first_and_last(sentence)&#13; All&#13; wait.&#13; &gt;&gt;&gt; ex25.print_first_and_last_sorted(sentence)&#13; All&#13; who&#13; &gt;&gt;&gt; ^D&#13; $&#13; </pre> </div> </td> </tr></tbody></table> 我們來逐行分析一下每一步實現的是什么: - 在第 5 行你將自己的 ex25.py 執行了 import,和你做過的其它 import 一樣。在 import 的時候你不需要加 .py 后綴。這個過程里,你把 ex25.py 當做了一個“模組(module)”來使用,你在這個模組里定義的函數也可以直接調用出來。 - 第 6 行你創建了一個用來處理的“句子(sentence)”。 - 第 7 行你使用 ex25 調用你的第一個函數 ex25.break_words。其中的 . (dot, period)符號可以告訴 Python:“嗨,我要運行 ex25 里的哪個個叫 break_words 的函數!” - 第 8 行我們只是輸入 words,而 python 將在第 9 行打印出這個變量里邊有什么。看上去可能會覺得奇怪,不過這其實是一個“列表(list)”,你會在后面的章節中學到它。 - 10-11 行我們使用 ex25.sort_words 來得到一個排序過的句子。 - 13-16 行我們使用 ex25.print_first_word 和 ex25.print_last_word 將第一個和最后一個詞打印出來。 - 第 17 行比較有趣。我把 words 變量寫錯成了 wrods,所以 python 在 18-20 行給了一個錯誤信息。 - 21-22 行我們打印出了修改過的詞匯列表。第一個和最后一個單詞我們已經打印過了,所以在這里沒有再次打印出來。 剩下的行你需要自己分析一下,就留作你的加分習題了。 ### 加分習題 1. 研究答案中沒有分析過的行,找出它們的來龍去脈。確認自己明白了自己使用的是模組 ex25 中定義的函數。 1. 試著執行 help(ex25) 和 help(ex25.break_words) 。這是你得到模組幫助文檔的方式。 所謂幫助文檔就是你定義函數時放在 """ 之間的東西,它們也被稱作 documentationcomments (文檔注解),后面你還會看到更多類似的東西。 1. 重復鍵入 ex25. 是很煩的一件事情。有一個捷徑就是用 fromex25import* 的方式導入模組。這相當于說:“我要把 ex25 中所有的東西 import 進來。”程序員喜歡說這樣的倒裝句,開一個新的會話,看看你所有的函數是不是已經在那里了。 1. 把你腳本里的內容逐行通過 python 編譯器執行,看看會是什么樣子。你可以執行CTRL-D (Windows 下是 CTRL-Z)來關閉編譯器。
                  <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>

                              哎呀哎呀视频在线观看