rob*_*don 4 python shell interactive undefined
我正在解决这个问题的答案:Python Interactive Shell Type Application
我的代码看起来像这样
def main():
while True:
s = input('> ')
if s == 'hello':
print('hi')
if s == 'exit':
break
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
如果我运行它,并键入你好,我得到
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
Run Code Online (Sandbox Code Playgroud)
我应该如何监听文本,并根据结果调用不同的函数?
您在Python 2.x下运行它,其中input()实际上评估您键入的内容作为Python表达式.因此,它正在寻找一个名为的变量hello,并且,由于您没有定义一个变量,它会抛出错误.要么使用Python 3.x,要么使用raw_input().
从你的括号中print我假设你打算在Python 3.x下运行它.
| 归档时间: |
|
| 查看次数: |
2768 次 |
| 最近记录: |