4

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?
0

3 Answers 3

5

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.

8
  • the above python script works for me superbly. Jun 16, 2014 at 12:54
  • @AvinashRaj Yes, but not when called by python script.py I get the same error as OP! Jun 16, 2014 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. Jun 16, 2014 at 12:59
  • @AvinashRaj sure, but try calling it by python, enter "a"... Jun 16, 2014 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, 2014 at 18:06
2

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

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .