如何检查变量是否为数字(没有数字)并在不在 Python 中时引发错误?
How do you check if a variable is numeric (without isnumeric) and raise error if it is not in Python?
我不明白为什么当半径或高度是浮点数时以下内容无法正确运行并引发错误。
def cone(radius, height):
if isinstance(radius, int) or isinstance(radius,float) == False:
raise TypeError("Error: parameters radius and height must be numeric.")
if isinstance(height, int) or isinstance (height,float)== False:
raise TypeError("Error: parameters radius and height must be numeric.")
if radius > 0 and height > 0:
return ((radius*radius)*(3.1415)*(height/3))
if radius<=0:
raise ValueError("Error: radius must be positive.")
if height <=0:
raise ValueError("Error: height must be positive.")
看来你想要
if not (isinstance(radius, int) or isinstance(radius,float)):
或者实际上
if not isinstance(radius, (float, int)):
目前你的逻辑是这样的
if isinstance(radius, int) or (isinstance(radius,float) == False):
所以,如果你得到一个整数,你就会得到错误。如果你得到一个浮点数,你就不会出错,因为你最终得到 False or (True == False)
任何 or False
与 bool(Anything)
相同,在本例中为 True == False
,即 False
此外,我建议首先提出所有错误并检查条件。
然后 return 实际的数学运算,因为那时变量不可能为正
您的问题是 if
被评估为:
if (isinstance(radius, int)) or (isinstance(radius,float) == False)
我想这不是你的意思。
无论如何,您实际上可以使用 try/except
使您的代码更简单。您可以 假设 您的参数是数字,然后与 0
进行比较。如果不是,将引发异常以便您可以捕获它:
def cone(radius, height):
try:
if radius > 0 and height > 0:
return ((radius*radius)*(3.1415)*(height/3))
else:
raise ValueError("Error: radius and height must be positive.")
except TypeError:
raise TypeError("Error: parameters radius and height must be numeric.")
你可以传递多种类型给isinstance
,这样你就可以去掉or
:
def cone(radius, height):
if not isinstance(radius, (float, int)):
raise TypeError("Error: parameters radius and height must be numeric.")
if not isinstance(height, (float, int)):
raise TypeError("Error: parameters radius and height must be numeric.")
if radius > 0 and height > 0:
return ((radius*radius)*(3.1415)*(height/3))
if radius<=0:
raise ValueError("Error: radius must be positive.")
if height <=0:
raise ValueError("Error: height must be positive.")
for value in [(1, 2), (0.33, 'foo')]:
print(cone(*value))
输出:
0.0
Traceback (most recent call last):
File "/private/tmp/s.py", line 15, in <module>
print(cone(*value))
File "/private/tmp/s.py", line 5, in cone
raise TypeError("Error: parameters radius and height must be numeric.")
TypeError: Error: parameters radius and height must be numeric.
我不明白为什么当半径或高度是浮点数时以下内容无法正确运行并引发错误。
def cone(radius, height):
if isinstance(radius, int) or isinstance(radius,float) == False:
raise TypeError("Error: parameters radius and height must be numeric.")
if isinstance(height, int) or isinstance (height,float)== False:
raise TypeError("Error: parameters radius and height must be numeric.")
if radius > 0 and height > 0:
return ((radius*radius)*(3.1415)*(height/3))
if radius<=0:
raise ValueError("Error: radius must be positive.")
if height <=0:
raise ValueError("Error: height must be positive.")
看来你想要
if not (isinstance(radius, int) or isinstance(radius,float)):
或者实际上
if not isinstance(radius, (float, int)):
目前你的逻辑是这样的
if isinstance(radius, int) or (isinstance(radius,float) == False):
所以,如果你得到一个整数,你就会得到错误。如果你得到一个浮点数,你就不会出错,因为你最终得到 False or (True == False)
任何 or False
与 bool(Anything)
相同,在本例中为 True == False
,即 False
此外,我建议首先提出所有错误并检查条件。
然后 return 实际的数学运算,因为那时变量不可能为正
您的问题是 if
被评估为:
if (isinstance(radius, int)) or (isinstance(radius,float) == False)
我想这不是你的意思。
无论如何,您实际上可以使用 try/except
使您的代码更简单。您可以 假设 您的参数是数字,然后与 0
进行比较。如果不是,将引发异常以便您可以捕获它:
def cone(radius, height):
try:
if radius > 0 and height > 0:
return ((radius*radius)*(3.1415)*(height/3))
else:
raise ValueError("Error: radius and height must be positive.")
except TypeError:
raise TypeError("Error: parameters radius and height must be numeric.")
你可以传递多种类型给isinstance
,这样你就可以去掉or
:
def cone(radius, height):
if not isinstance(radius, (float, int)):
raise TypeError("Error: parameters radius and height must be numeric.")
if not isinstance(height, (float, int)):
raise TypeError("Error: parameters radius and height must be numeric.")
if radius > 0 and height > 0:
return ((radius*radius)*(3.1415)*(height/3))
if radius<=0:
raise ValueError("Error: radius must be positive.")
if height <=0:
raise ValueError("Error: height must be positive.")
for value in [(1, 2), (0.33, 'foo')]:
print(cone(*value))
输出:
0.0
Traceback (most recent call last):
File "/private/tmp/s.py", line 15, in <module>
print(cone(*value))
File "/private/tmp/s.py", line 5, in cone
raise TypeError("Error: parameters radius and height must be numeric.")
TypeError: Error: parameters radius and height must be numeric.