Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raise error
#1
I am testing the function __truediv__ in this code, I am specifically interested in the
raise ValueError
command.

Is supposed to raise an error in case the division leads to + or - infinity.

This code instead raises the following error:

Error:
File "C:/Users/Desktop/python ode/homework22.py", line 53, in <module> cc = aa/bb TypeError: unsupported operand type(s) for /: 'interval' and 'interval'
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)
Reply
#2
(May-09-2019, 07:55 PM)mcgrim Wrote: I am testing the function __truediv__ in this code
well, 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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I left my code untouched with the difference of replacing __div__ with __truediv__,
but now I am getting a different kind of error:

Error:
File "C:/Users/Desktop/python ode/homework22.py", line 41, in __truediv__ a, b, c, d = self.lo, self.up, other.lo, other.up AttributeError: 'interval' object has no attribute 'lo'
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.
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.up
what you try is to assign the value of self.lo to a
I 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 error
What do you mean?

sorry, while replying I think I answered myself, lol !
I got it now, thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have 'Failed: DID NOT RAISE <class 'Exception'>' williamKrakos 3 671 Feb-20-2024, 09:46 PM
Last Post: williamKrakos
Smile Exception with raise and input artemisellada 3 2,500 Apr-23-2021, 08:19 PM
Last Post: jefsummers
  Raise an exception for syntax error sbabu 8 3,157 Feb-10-2020, 01:57 AM
Last Post: sbabu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020