I have a simple Python program that asks yes or no question and I validate that input. If I run this Python shell, it runs fine. If I enter invalid characters it loops back to top of while.
我有一个简单的Python程序,询问是或否问题,我验证了输入。如果我运行这个Python shell,它运行正常。如果我输入无效字符,它会循环回到while。
However, if I run this in the terminal window and try to enter an invalid character it errors as shown below.
但是,如果我在终端窗口中运行此命令并尝试输入无效字符,则会出现错误,如下所示。
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'
2 个解决方案
#1
I can clearly see in the interactive shell you working in python 3.2.3 (background). But I can not see the python version you're running from the command line (foreground).
我可以在交互式shell中清楚地看到你在python 3.2.3(后台)中工作。但我看不到你从命令行(前台)运行的python版本。
On your raspberrypi, execute this command from the shell:
在您的raspberrypi上,从shell执行以下命令:
python --version
I am expecting to see python 2.x here, because the behaviour of input()
differs between python 2 and python 3, in a way that would cause exactly the behaviour you have seen.
我期待在这里看到python 2.x,因为input()的行为在python 2和python 3之间有所不同,这种方式会导致你看到的行为。
You might want to add a line like
您可能想要添加一行
#!/usr/bin/env python3
To the top of your .py
file, and then chmod +x
on it. Afterward you should be able to execute it directly (./guipy01.py
) and the correct python interpreter will be selected automatically.
到你的.py文件的顶部,然后chmod + x。之后你应该能够直接执行它(./guipy01.py)并自动选择正确的python解释器。
#2
Looks like your RPi is using Python 2; the input
function does an eval
there.input
in Python 3 is equivalent to raw_input
in Python 2. (See PEP-3111)
看起来你的RPi正在使用Python 2;输入函数在那里做了一个eval。 Python 3中的输入等同于Python 2中的raw_input。(参见PEP-3111)
Ideally, you should change your RPi interpreter to Python 3. Failing that, you can make it version-agnostic like so:
理想情况下,您应该将RPi解释器更改为Python 3.如果不这样做,您可以使其与版本无关:
try:
input = raw_input
except NameError:
pass
#1
I can clearly see in the interactive shell you working in python 3.2.3 (background). But I can not see the python version you're running from the command line (foreground).
我可以在交互式shell中清楚地看到你在python 3.2.3(后台)中工作。但我看不到你从命令行(前台)运行的python版本。
On your raspberrypi, execute this command from the shell:
在您的raspberrypi上,从shell执行以下命令:
python --version
I am expecting to see python 2.x here, because the behaviour of input()
differs between python 2 and python 3, in a way that would cause exactly the behaviour you have seen.
我期待在这里看到python 2.x,因为input()的行为在python 2和python 3之间有所不同,这种方式会导致你看到的行为。
You might want to add a line like
您可能想要添加一行
#!/usr/bin/env python3
To the top of your .py
file, and then chmod +x
on it. Afterward you should be able to execute it directly (./guipy01.py
) and the correct python interpreter will be selected automatically.
到你的.py文件的顶部,然后chmod + x。之后你应该能够直接执行它(./guipy01.py)并自动选择正确的python解释器。
#2
Looks like your RPi is using Python 2; the input
function does an eval
there.input
in Python 3 is equivalent to raw_input
in Python 2. (See PEP-3111)
看起来你的RPi正在使用Python 2;输入函数在那里做了一个eval。 Python 3中的输入等同于Python 2中的raw_input。(参见PEP-3111)
Ideally, you should change your RPi interpreter to Python 3. Failing that, you can make it version-agnostic like so:
理想情况下,您应该将RPi解释器更改为Python 3.如果不这样做,您可以使其与版本无关:
try:
input = raw_input
except NameError:
pass