我有一个简单的 Python 程序,它会询问是或否的问题,然后我会验证该输入。 如果我运行这个 Python shell,它运行良好。如果我输入无效字符,它会循环回到顶部。
但是,如果我在终端窗口中运行它并尝试输入无效字符,则会出现如下所示的错误。
endProgram = 0
while endProgram != 1:
userInput = input("Yes or No? ");
userInput = userInput.lower();
while userInput not in ['yes', 'no']:
print("Try again.")
break
endProgram = userInput == 'no'
请您参考如下方法:
看起来您的树莓派正在使用 Python 2; input 函数在那里执行 eval。
Python 3 中的 input 等同于 Python 2 中的 raw_input。(参见 PEP-3111 )
理想情况下,您应该将 RPi 解释器更改为 Python 3。否则,您可以像这样使其与版本无关:
try:
input = raw_input
except NameError:
pass
