Python 之 input 與 raw_input 的區別

 input 接受合法的Python 表達式

     raw_input 將所有的輸入作爲原始數據,將其放入字符串中

     >>> name =  input("what's your name ?")

                    what's your name ?  Yellow
                    Traceback (most recent call last):
                    File "<pyshell#10>", line 1, in <module>
                    name = input("What's is your name ?")
                    File "<string>", line 1, in <module>
                    NameError: name 'yellow' is not defined

    >>> Yellow = "yellow"
    >>> name =  input("what's your name ?")

                    what's your name ?  Yellow

    >>> print "Hello, " + name
                    Hello, yellow

>>> input('Enter a number ')
                Enter a number 3
                3

             第一次輸入“Yellow”時,作爲表達式,但是沒有定義,故報錯,如果輸入爲正確表達式,則需要加單引號或雙引號,作爲字符串表達式輸入。
             第二次定義了Yellow爲表達式,並進行了賦值操作,所以再次輸入,爲合法的表達式,故沒有報錯。
             第三次輸入數字3,作爲數字,即合法的表達式,故沒有報錯。

             raw_input() 函數將所有輸入原始數據,並放入字符串中,故不會報錯。

             >>> name = raw_input("What's is your name ?")
                            What's is your name ?Yellow
            >>> print "Hello, " + name
                            Hello, Yellow
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章