Reputation: 15475
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.
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'
Upvotes: 3
Views: 734
Reputation: 363233
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).
On your raspberrypi, execute this command from the 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.
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.
Upvotes: 3
Reputation: 47840
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)
Ideally, you should change your RPi interpreter to Python 3. Failing that, you can make it version-agnostic like so:
try:
input = raw_input
except NameError:
pass
Upvotes: 4