![]() |
raise error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: raise error (/thread-18222.html) |
raise error - mcgrim - May-09-2019 I am testing the function __truediv__ in this code, I am specifically interested in the raise ValueErrorcommand. Is supposed to raise an error in case the division leads to + or - infinity. This code instead raises the following error: I am not sure what is wrong, since everything else is working.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((a-d),(b-c)) 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: 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) I = interval aa = I(-3,-2) bb = I(0,5) cc = aa/bb print(aa,bb) print(cc) RE: raise error - buran - May-09-2019 (May-09-2019, 07:55 PM)mcgrim Wrote: I am testing the function __truediv__ in this codewell, there is no __truediv__() method/function in this code also, on line 17 you use self.lo, self.up , other.lo , other.up but these attributes are never defined/assigned in your code
RE: raise error - mcgrim - May-09-2019 I left my code untouched with the difference of replacing __div__ with __truediv__, but now I am getting a different kind of error: I am not sure what you mean by this:Quote: these attributes are never defined/assigned in your code as I set them as a,b,c,d and I use these characters in my code. Perhaps I am misunderstanding you. RE: raise error - buran - May-09-2019 I mean exactly what error says: 'interval' object has no attribute 'lo' (and 'up'). In other words where in the class definition you define attributes lo and up - nowhere. (May-09-2019, 08:15 PM)mcgrim Wrote: as I set them as a,b,c,d and I use these characters in my code.I've noticed that you have wrong understanding of assignment. you don't assign a, b, c and d to self.lo, self.up, other.lo and other.up what you try is to assign the value of self.lo to a, etc. (this particular assignment is called unpacking, but that is detail - the important is that you [try] to assign the value of self.lo to a) and because there is no attribute interval.lo you get this error RE: raise error - mcgrim - May-09-2019 I am having an hard time understanding you. Quote:you don't assign a, b, c and d to self.lo, self.up, other.lo and other.upI am trying to assign self.left to a, self.right to b, other.left to c and other.right to d. is this wrong? Quote:and because there is no attribute interval.lo you get this errorWhat do you mean? sorry, while replying I think I answered myself, lol ! I got it now, thanks. |