Discuss / Python / input和print合用疑问

input和print合用疑问

Topic source

python3.6.4,mac10.13.1下运行终端,执行name.py,代码如下: name=input('please enter your name') print(name)

终端出现please enter your name后 1/ 输入rfvon,显示 Traceback (most recent call last): File "/Users/rfvon/Desktop/test/name.py", line 1, in <module> name=input('please enter your name') File "<string>", line 1, in <module> NameError: name 'rfvon' is not defined

2/ 后来查询资料,建议如下:Enter your name as a string eg 'Tom'. Alternatively, use raw_input instead of input 输入‘rfvon’,正常显示rfvon 换用raw_input,python3.6.4中显示

name=raw_input('please enter your name') Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> name=raw_input('please enter your name') NameError: name 'raw_input' is not defined

有两点疑问,求大神解答 1/为什么终端加‘’后可以输出? 2/为什么不能使用raw_input?

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

第一个程序你写得有问题,或者自己运行的程序和贴出来的不是同一个

raw_input已经在py3中废弃了,你查的资料太古老

非常感谢大大解答! name.py在IDLE运行是正确的,我没有注意到Mac 终端的python2.7.10,终端的版本不适用于编译python3.6.4。 哈哈哈,确实还是太古老了。


  • 1

Reply