Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

#!/usr/bin/python 
x = input('Enter your number:  ')     
try:
    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 "cycle.py", 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
add comment

3 Answers 3

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/script.py

and it will run fine :)

Explanation:

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 at 12:54
    
@AvinashRaj Yes, but not when called by python script.py I get the same error as OP! –  Jacob Vlijm Jun 16 at 12:56
3  
@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 at 12:59
    
@AvinashRaj sure, but try calling it by python, enter "a"... –  Jacob Vlijm Jun 16 at 12:59
1  
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 at 18:06
show 4 more comments

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 https://docs.python.org/2.7/library/functions.html?highlight=input#input

share|improve this answer
add comment

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

#!/usr/bin/python 
try:
    x=input('Enter your number:  ')
    x=float(x)
    print('Your number multiplied by 2 is:  ', x*2)
except:
    print('not a valid choice')
    x = 0
share|improve this answer
add comment

Your Answer

 
discard

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.