[英]Handling similar exceptions for multiple variables
How do I handle the exceptions in such a way that I get a separate comment for the same exception with two different variables.如何以这样的方式处理异常,即我获得针对具有两个不同变量的相同异常的单独注释。
try:
global a
a = int(num)
except ValueError:
print(num, "is not a number. Please try again...")
try:
global b
b= int(denom)
return (Fraction(a, b))
except ValueError:
print(denom, "is not a number. Please try again...")
How do I add both the variables in the same try block but have two different exception blocks as I have two print statements.如何在同一个 try 块中添加两个变量但有两个不同的异常块,因为我有两个打印语句。 The issue with the above code is that if I input a string instead of number for variable "a", the system prints the statement but throws the below error too:
上面代码的问题是,如果我为变量“a”输入一个字符串而不是数字,系统会打印该语句,但也会引发以下错误:
NameError: name 'a' is not defined NameError:名称“a”未定义
Also, where do I add the return function?另外,我在哪里添加返回 function?
this will help you handle your exception这将帮助您处理您的异常
def Fraction(a,b):
try:
assert type(a) ==int, "is not a number. Please try again..."
assert type(b)==int, "is not a number. Please try again..."
.........
except AssertionError as msg:
print(msg)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.