我想在代码中发生错误时使用异常参数来显示附加信息。 这是我的代码。
ArgumentOfAnException.py
from pip._vendor.distlib.compat import raw_input
def to_celsius(f):
try:
c = (f-32)*5/9;
return c;
except ValueError, Argument:
print("The argumet doesn't contain number\n", Argument);
return;
f = raw_input("Enter temperature in *F : ");
print("Temperature in Celsius : ", to_celsius(float(f)));
print("Thank you...");
这里我使用参数变量在代码中发生错误时显示附加信息,但运行程序后会显示语法错误 在控制台输出中,错误如下
File "F:\Document\Files\Coding\Java Eclipse\Python Project\First Project\src\Error\ArgumentOfAnException.py", line 7
except ValueError, Argument:
^
SyntaxError: invalid syntax
最佳答案
您似乎使用的是 python 3.x。
使用以下语法(使用as
):
except ValueError as Argument:
关于Python:使用异常参数时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29971102/