3

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'

enter image description here

1
  • 2
    Are you using the same Python version on both? input changes behavior between 2 and 3. Commented May 7, 2015 at 2:42

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.