小编VDV*_*VDV的帖子

Bash/Python 错误处理?

我正在尝试将包含错误处理的非常基本的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
Run Code Online (Sandbox Code Playgroud)

这是来自 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
Run Code Online (Sandbox Code Playgroud)

据我了解,输入错误必须首先由 Python 处理,然后它会向 Bash 返回 0 退出状态,但显然情况并非如此?

  1. 我的代码没问题吗?
  2. 有没有办法强制 …

python bash

4
推荐指数
1
解决办法
3596
查看次数

标签 统计

bash ×1

python ×1