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?
- Is my code all right?
- Is there a way to force Python to handle the error first without evoking Bash?
- Is there any other critical pitfalls when running Python programs (presumably correctly written) as Bash scripts?