我是 python 新手,一直在玩,但我在 mac 上遇到错误
下面是我在 input.py 文件中的代码
person = input('Enter your name: ')
print('Hello', person)
如果我运行代码,这就是我的输出和错误
Johnathans-iMac:scripts johnathansmith$ python input.py
Enter your name: John
Traceback (most recent call last):
File "input.py", line 1, in <module>
person = input('Enter your name: ')
File "<string>", line 1, in <module>
NameError: name 'John' is not defined
请记住我是新人......这会是什么?
最佳答案
我认为您使用的是 Python 2.7。原因是,在此版本的 Python 中,input
函数会向用户请求一个字符串,然后尝试计算该字符串。由于代码中未定义 John
,Python 会提示。
尝试运行 python3
,或更改为使用 raw_input
而不是 input
。
使用 Python 3(交互式 session 的输出):
> person = input('Enter name:')
Enter name:John
> print('Hey', person)
Hey John
使用 Python 2:
> person = raw_input('Enter name:')
Enter name:John
> print 'Hey', person
Hey John
关于Mac 下的 python 输入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42304421/