py2 py3 ①input() --> eval(input) #eval()作用:把字符串当作变量 py2: >>> var=input("please input your name:") please input your name:hy Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name ‘hy‘ is not defined >>> var=input("please input your name:") please input your name:"hy" ##输入字符串或者字符的时候,要用双引号或者单引号包起来 >>> var ‘hy py2: >>> var=input("please input your name:") please input your name:yaobin Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name ‘yaobin‘ is not defined >>> yaobin="hy" >>> var=input("please input your name:") please input your name:yaobin >>> var ‘hy‘ ##py2的input()是一个特殊的raw_input(),只是外层调用了eval()函数而已。 py3: >>> eval(input("your name:")) your name:yaobin Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name ‘yaobin‘ is not defined >>> yaobin="hy" >>> eval(input("your name:")) your name:yaobin ‘hy‘ py2 py3 ②raw_input --> input() py2: >>> raw_input("your name:") your name:abc #不管你输入什么样的类型,都会转变成字符串 ‘abc‘ py3: >>> input("your name:") your name:abc ‘abc‘ >>>
时间: 2024-11-14 15:03:10