Python之input与raw_input的区别-成都快上网建站

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 "", line 1, in 
                    name = input("What's is your name ?")
                    File "", line 1, in 
                    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

本文标题:Python之input与raw_input的区别
本文URL:http://kswjz.com/article/jpgoce.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流