May-07-2019, 08:00 PM
given this code, I should extend
the class so that it can be initialized with only one real value.
for instance,
print(interval(1))
should print [1,1].
This is the code (which is the same as my previous task),
but I have no Idea how to change it, as the code the way it is now,
it produces the following error:
the class so that it can be initialized with only one real value.
for instance,
print(interval(1))
should print [1,1].
This is the code (which is the same as my previous task),
but I have no Idea how to change it, as the code the way it is now,
it produces the following error:
Error:line 183, in <module>
print(interval(1))
TypeError: __init__() missing 1 required positional argument: 'right'
Any help/hint?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 __truediv__(self, other): a, b, c, d = self.left, self.right, other.left, other.right # [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 __contains__(self,other): return (other>=self.left and other<=self.right) def __repr__(self): return '[{},{}]'.format(self.left,self.right) print(interval(1))