Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
division error
#1
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.
Reply
#2
There are less than 52 lines, how could there be an error on line 52?
Reply
#3
it looks like that the error is contained within the command __truediv__,
therefore might not have anything to do directly with the code I wrote.
Reply
#4
We need the full error message and we need the file name of the above code in order to see where it fails.
Reply
#5
this is everything that is printed out after I run it.

Error:
runfile('C:/UsersDesktop/python ode/extrial.py', wdir='C:/Users/Desktop/python ode') [-1, 3] [2, 6] [-4, -1] Traceback (most recent call last): File "<ipython-input-779-717f169c285d>", line 1, in <module> runfile('C:/Users/Desktop/python ode/extrial.py', wdir='C:/Users/Desktop/python ode') File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Desktop/python ode/extrial.py", line 111, in <module> print(I1/I2) 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
Reply
#6
This code cannot possible run, because you class definition is with small i - class interval and you instantiate Interval

also what is homework2.py?
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
#7
solved, the problem was the capitalization.
Thanks a lot.
That is the name of the file.
Reply
#8
(May-07-2019, 11:08 AM)mcgrim Wrote: That is the name of the file.
If that's the name of the file, what is extrial.py?
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
#9
sorry, I involuntarily created some confusion.
I have two different files next to each other, and I must have copy/pasted
from the other one.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  1 dimension Matrix Right Division theo_taison1 1 3,938 Jan-25-2017, 04:51 PM
Last Post: stranac

Forum Jump:

User Panel Messages

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