I want to raise an Value Error for negative values of a and b. I have played around with this but it raises ValueError for different values and it does not seem to work properly.
class RightTriangle:
def __init__(self, a, b):
self.a = a
self.b = b
self.c = np.sqrt(a**2+b**2)
if (self.a or self.b) < 0:
raise ValueError("Negative")
Fix:
if self.a < 0 or self.b < 0:
What you've got is evaluating (self.a or self.b)
first. What this first part is doing is this:
if self.a: # anything but 0 is True
return self.a
else:
return self.b
So, you're probably going to get self.a
most of the time, and then it's going to be compared in < 0
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.