<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    習題 13: 參數、解包、變量?

                    在這節練習中,我們將降到另外一種將變量傳遞給腳本的方法(所謂腳本,就是你寫的 .py 程序)。你已經知道,如果要運行 ex13.py,只要在命令行運行 python ex13.py 就可以了。這句命令中的 ex13.py 部分就是所謂的“參數(argument)”,我們現在要做的就是寫一個可以接受參數的腳本。

                    將下面的程序寫下來,后面你將看到詳細解釋。

                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    from sys import argv
                    
                    script, first, second, third = argv
                    
                    print "The script is called:", script
                    print "Your first variable is:", first
                    print "Your second variable is:", second
                    print "Your third variable is:", third
                    

                    在第 1 行我們有一個“import”語句. 這是你將 python 的功能引入你的腳本的方法. Python 不會一下子將它所有的功能給你,而是讓你需要什么就調用什么。這樣可以讓你的程序保持精簡,而后面的程序員看到你的代碼的時候,這些“import”可以作為提示,讓他們明白你的代碼用到了哪些功能。

                    argv 是所謂的“參數變量(argument variable)”,是一個非常標準的編程術語。在其他的編程語言里你也可以看到它。這個變量包含了你傳遞給 Python 的參數。通過后面的練習你將對它有更多的了解。

                    第 3 行將 argv “解包(unpack)”,與其將所有參數放到同一個變量下面,我們將每個參數賦予一個變量名: script, first, second, 以及 third。這也許看上去有些奇怪, 不過”解包”可能是最好的描述方式了。它的含義很簡單:“把 argv 中的東西解包,將所有的參數依次賦予左邊的變量名”。

                    接下來就是正常的打印了。

                    等一下!“功能”還有另外一個名字?

                    前面我們使用 import 讓你的程序實現更多的功能,但實際上沒人吧 import 稱為“功能”。我希望你可以在沒接觸到正式術語的時候就弄懂它的功能。在繼續下去之前, 你需要知道它們的真正名稱:模組(modules)

                    從現在開始我們將把這些我們導入(import)進來的功能稱作模組。你將看到類似這樣的說法:“你需要把 sys 模組 import 進來。”也有人將它們稱作“庫(libraries)”,不過我們還是叫它們模組吧。

                    你應該看到的結果?

                    用下面的方法運行你的程序(注意你必須傳遞*三*個參數):

                    python ex13.py first 2nd 3rd
                    

                    如果你每次使用不同的參數運行,你將看到下面的結果:

                    $ python ex13.py first 2nd 3rd
                    The script is called: ex13.py
                    Your first variable is: first
                    Your second variable is: 2nd
                    Your third variable is: 3rd
                    
                    $ python ex13.py cheese apples bread
                    The script is called: ex13.py
                    Your first variable is: cheese
                    Your second variable is: apples
                    Your third variable is: bread
                    
                    $ python ex13.py Zed A. Shaw
                    The script is called: ex13.py
                    Your first variable is: Zed
                    Your second variable is: A.
                    Your third variable is: Shaw
                    

                    你其實可以將“first”、“2nd”、“3rd”替換成任意三樣東西。你可以將它們換成任意你想要的東西.

                    python ex13.py stuff I like
                    python ex13.py anything 6 7
                    

                    如果你沒有運行對,你將看到如下錯誤:

                    python ex13.py first 2nd
                    Traceback (most recent call last):
                      File "ex/ex13.py", line 3, in <module>
                        script, first, second, third = argv
                    ValueError: need more than 3 values to unpack
                    
                    當你運行腳本時提供的參數的個數不對的時候,你就會看到上述錯誤信息 (這次我只用了
                    first 2nd 兩個參數)。“need more than 3 values to unpack”這個錯誤信息告訴你參數數量不足。

                    加分習題?

                    1. 給你的腳本三個以下的參數。看看會得到什么錯誤信息。試著解釋一下。
                    2. 再寫兩個腳本,其中一個接受更少的參數,另一個接受更多的參數,在參數解包時給它們取一些有意義的變量名。
                    3. raw_inputargv 一起使用,讓你的腳本從用戶手上得到更多的輸入。
                    4. 記住“模組(modules)”為你提供額外功能。多讀幾遍把這個詞記住,因為我們后面還會用到它。

                    Project Versions

                    Table Of Contents

                    Previous topic

                    習題 12: 提示別人

                    Next topic

                    習題 14: 提示和傳遞

                    This Page

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频