Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to execute very basic Python code containing error handling as a Bash script, but while the code seems to run OK in Python, the code generates problems when executed under Bash.

x = input('Enter your number:  ')     
    x = float(x)
    print('Your number multiplied by 2 is:  ', x*2) 
except ValueError:
    print('not a valid choice')
    x == 0

This is the error report from Bash:

Enter your number:  -p Traceback (most recent call last):
  File "", line 3, in <module>
    x=input('Enter your number:  ')
  File "<string>", line 1, in <module>
NameError: name 'p' is not defined

As I understand the input error had to be handled by Python first and then it would return 0 exit status to Bash, but apparently this is not the case?

  1. Is my code all right?
  2. Is there a way to force Python to handle the error first without evoking Bash?
  3. Is there any other critical pitfalls when running Python programs (presumably correctly written) as Bash scripts?
share|improve this question
up vote 5 down vote accepted

You write your code in Python 3 (looking at "print"), but the shebang suggests Python 2. Change the shebang to

#!/usr/bin/env python3

and run it by:

python3 /path/to/

and it will run fine :)


As hinted in Florian Diesch' comment, input() has changed in Python 3:

In Python 2, input() tries to use the input as a Python expression (like eval()), while in Python 3, input() replaces the raw_input() from Python 2.

share|improve this answer
the above python script works for me superbly. – Avinash Raj Jun 16 '14 at 12:54
@AvinashRaj Yes, but not when called by python I get the same error as OP! – Jacob Vlijm Jun 16 '14 at 12:56
@Avinash Raj: You get this error if you enter p instead of a number if you use Python2 but not if you use Python3 because input does a different thing. – Florian Diesch Jun 16 '14 at 12:59
@AvinashRaj sure, but try calling it by python, enter "a"... – Jacob Vlijm Jun 16 '14 at 12:59
I've edited the shebang to use env. It's just a best-practice for Python which can have multiple versions on the system. Deferring to env means it should pick the right one. – Oli Jun 16 '14 at 18:06

I suggest you raw_input function than input.

input([prompt]) -> raw_input([prompt])

Equivalent to eval(raw_input(prompt)).

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Consider using the raw_input() function for general input from users.

You can see about it at

share|improve this answer

Another way to fix it, take input inside try clause. It's better except any error. x=0 I think you mean assignment there. This way it can accept, values like: 2*3

    x=input('Enter your number:  ')
    print('Your number multiplied by 2 is:  ', x*2)
    print('not a valid choice')
    x = 0
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.