此函数newdef()在解释器中完美运行,但在作为文件执行时会抛出NameError。有人可以告诉我为什么吗?
myuser$ cat r2.py
#!/usr/bin/env python
def newdef():
a = input('say yes\n')
if a == 'yes':
print('he said yes')
else:
print('he said something else')
newdef()
从命令行执行:
myuser$ ./r2.py
say yes
yes
Traceback (most recent call last):
File "./r2.py", line 11, in <module>
newdef()
File "./r2.py", line 4, in newdef
a = input('say yes\n')
File "<string>", line 1, in <module>
NameError: name 'yes' is not defined
myuser$
如果我将函数复制/粘贴到iPython解释器中并在那里执行它就可以了。