我正在尝试执行非常基本的Python代码,其中包含作为Bash脚本进行错误处理的代码,但是尽管该代码在Python中似乎可以正常运行,但是在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
这是来自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
据我了解,输入错误必须首先由Python处理,然后它将向Bash返回0退出状态,但是显然不是这样吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句