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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 練習25. 更多更多的練習 我們將做一些關于函數和變量的練習,以確認你真正掌握了這些知識。這節練習對你來說可以說是:寫程序,逐行研究,弄懂它。 過這節練習還是有些不同,你不需要運行它,取而代之,你需要將它導入到 python 里通過自己執行函數的方式運行。 ~~~ def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') return words def sort_words(words): """Sorts the words.""" return sorted(words) def print_first_word(words): """Prints the first word after popping it off.""" word = words.pop(0) print word def print_last_word(words): """Prints the last word after popping it off.""" word = words.pop(-1) print word def sort_sentence(sentence): """Takes in a full sentence and returns the sorted words.""" words = break_words(sentence) return sort_words(words) def print_first_and_last(sentence): """Prints the first and last words of the sentence.""" words = break_words(sentence) print_first_word(words) print_last_word(words) def print_first_and_last_sorted(sentence): """Sorts the words then prints the first and last one.""" words = sort_sentence(sentence) print_first_word(words) print_last_word(words) ~~~ 首先以正常的方式`python ex25.py`運行,找出里邊的錯誤,并修正。然后你需要跟著下面的答案部分完成這節練習。 ## 你看到的結果 在這節練習中,我們將在Python解析器中,以交互的方式和你寫的`ex25.py`文件交流,你可以像下面這樣在命令行中啟動python解析器: ~~~ $ python Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> ~~~ 你的輸出應該和我類似,在`>`符號之后,你可以輸入并立即執行python代碼。我希望你用這種方式逐行輸入下方的python代碼,并看看他們有什么用: ~~~ import ex25 sentence = "All good things come to those who wait." words = ex25.break_words(sentence) words sorted_words = ex25.sort_words(words) sorted_words ex25.print_first_word(words) ex25.print_last_word(words) words ex25.print_first_word(sorted_words) ex25.print_last_word(sorted_words) sorted_words sorted_words = ex25.sort_sentence(sentence) sorted_words ex25.print_first_and_last(sentence) ex25.print_first_and_last_sorted(sentence) ~~~ 這是我做出來的樣子: ~~~ Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import ex25 >>> sentence = "All good things come to those who wait." >>> words = ex25.break_words(sentence) >>> words ['All', 'good', 'things', 'come', 'to', 'those', 'who', 'wait.'] >>> sorted_words = ex25.sort_words(words) >>> sorted_words ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who'] >>> ex25.print_first_word(words) All >>> ex25.print_last_word(words) wait. >>> words ['good', 'things', 'come', 'to', 'those', 'who'] >>> ex25.print_first_word(sorted_words) All >>> ex25.print_last_word(sorted_words) who >>> sorted_words ['come', 'good', 'things', 'those', 'to', 'wait.'] >>> sorted_words = ex25.sort_sentence(sentence) >>> sorted_words ['All', 'come', 'good', 'things', 'those', 'to', 'wait.', 'who'] >>> ex25.print_first_and_last(sentence) All wait. >>> ex25.print_first_and_last_sorted(sentence) All who ~~~ 當你寫完這些代碼,確保你能在`ex25.py`中找到運行的函數,并且明白它們每一個都是如何工作的。如果你的運行結果出錯或者跟我的結果不一樣,你就需要檢查并修復你的代碼,重啟python解析器,再次運行程序。 ## 附加題 > 1. 研究答案中沒有分析過的行,找出它們的來龍去脈。確認自己明白了自己使用的是模塊`ex25`中定義的函數。 > 1. 試著執行`help(ex25)`和`help(ex25.break_words)`。這是你得到模塊幫助文檔的方式。 所謂幫助文檔就是你定義函數時放在`"""`之間的東西,它們也被稱作`documentation comments (文檔注解)`,后面你還會看到更多類似的東西。 > 1. 重復鍵入`ex25.` 是很煩的一件事情。有一個捷徑就是用`from ex25 import *`的方式導入模組。這相當于說:“我要把 ex25 中所有的東西 import 進來。”程序員喜歡說這樣的倒裝句,開一個新的會話,看看你所有的函數是不是已經在那里了。 > 1. 把你腳本里的內容逐行通過python編譯器執行,看看會是什么樣子。你可以通過輸入`quit()`來退出Python。 ## 常見問題 ### Q: 我的某些函數沒有打印輸出任何值 > 你的函數末尾可能缺少`return`語句,檢查你的文件,確保每一行代碼的正確性。 ### Q: 我輸入`import ex25`的時候遇到報錯`import: command not found`. > 仔細觀察“你看到的結果”部分,看我是如何運行程序的。我是在python解析器里而不是在命令行運行程序。你應該先運行python解析器。 ### Q: 當我輸入`import ex25.py`的時候,我遇到報錯`ImportError: No module named ex25.py`. > 不要加上`.py`。Python知道文件是以`.py`結尾的,所以你只要輸入`import ex25`就可以了。 ### Q:運行程序是,遇到報錯信息`SyntaxError: invalid syntax` > 這個信息說明在報錯的這一行或之前的某一行你可能少寫了一個`(` 或者 `"` 或者其它的語法錯誤。當你遇到這個報錯的時候,從報錯的行開始,向上檢查是否每一行代碼都是正確的。 ### Q:函數`words.pop`是如何改變變量`words`的值的? > 這是一個復雜的問題,在這個實例中,`words`是一個列表,所以你可以調用它的一些命令,而它也會保留這些命令的結果。這類似于文件的工作原理。 ### Q: 什么情況下,我可以在函數中用`print`代替`return`? > `return`是從函數給出的代碼行調用的函數的結果。你可以把函數理解成 通過參數獲取輸入,并通過`return`返回輸出,而`print`是與這個過程完全無關的,它只負責在終端打印輸出。
                  <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>

                              哎呀哎呀视频在线观看