美文网首页
Python——区别input()和raw_input()

Python——区别input()和raw_input()

作者: xuluqxulu | 来源:发表于2017-07-20 20:31 被阅读0次

    raw_input() 将所有输入作为字符串看待,返回字符串类型。
    而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float )

    intput()

    print "Who do you think I am?"
    user = input("please input")
    print user
    
    

    如果这个时候输入 字符串 会报错

    ================= RESTART: C:\Python27\Tools\Scripts\xulu.py =================
    Who do you think I am?
    please input: swift
    
    Traceback (most recent call last):
      File "C:\Python27\Tools\Scripts\xulu.py", line 2, in <module>
        user = input("please input ")
      File "<string>", line 1, in <module>
    NameError: name "swift" is not defined
    

    如果加上引号就可以了

    ================ RESTART: C:\Python27\Tools\Scripts\xulu.py =================
    Who do you think I am?
    please input "swift"
    swift
    

    raw_input()

    print "Who do you think I am?"
    user = raw_input("please input: ")
    print user
    
    

    字符串不需要加引号

    ================= RESTART: C:\Python27\Tools\Scripts\xulu.py =================
    Who do you think I am?
    please input: swift
    swift
    

    python3.2.3中 input和raw_input 整合了,没有了raw_input*

    >>> user=input("please input:")  
    please input:wei  
    >>> user  
    'wei'  
    >>> user=input("please input:")                     #input的输出结果都是作为字符串  
    please input:123  
    >>> user  
    '123'  
    

    相关文章

      网友评论

          本文标题:Python——区别input()和raw_input()

          本文链接:https://www.haomeiwen.com/subject/khpjkxtx.html