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
相关文章
相关标签/搜索