May-06-2019, 09:25 PM
The following code
class interval: def __init__(self,left,right): self.right=right self.left=left def __add__(self,other): a,b,c,d = self.left, self.right, other.left, other.right return interval(a+c,b+d) def __sub__(self,other): a,b,c,d=self.left,self.right,other.left,other.right return interval(min(a,b)-max(c,d),max(a,b)-min(c,d)) def __mul__(self,other): a,b,c,d=self.left,self.right,other.left,other.right return interval(min(a*c, a*d, b*c, b*d), max(a*c, a*d, b*c, b*d)) def __div__(self, other): a, b, c, d = self.lo, self.up, other.lo, other.up # [c,d] cannot contain zero: print(min(a/c, a/d, b/c, b/d)) if c*d <= 0: raise ValueError ('Interval %s cannot be denominator because it contains zero' % other) return interval(min(a/c, a/d, b/c, b/d), max(a/c, a/d, b/c, b/d)) def __repr__(self): return '[{},{}]'.format(self.left,self.right) I1 = Interval(1, 4) I2 = Interval(-2, -1) print(I1 + I2) print(I1-I2) print(I1*I2) print(I1/I2)gives me the following error:
Error:File "C:/Users/Desktop/python ode/homework2.py", line 52, in __truediv__
raise ValueError('Please make sure values is of sort int and x !=0')
ValueError: Please make sure values is of sort int and x !=0
and I am not sure what to do to fix it.