This is my class and its method add:

- Code: Select all
`class Interval(object):`

def __init__(self, min, max=None): #task 6

if max is None:

max = min

self.min = min

self.max = max

def __repr__(self):

return '[{}, {}]'.format(self.min, self.max)

def __add__(self,other):

f1, s1 = self.min, self.max

if isinstance(other, int): #w

f2, s2 = other, 1 #w

else:

f2, s2 = other.min, other.max

return Interval(f1+f2, s1+s2)

I want to be able to count like this:

- Code: Select all
`print Interval(3,5)+1`

print 1 + Interval(3,5)

print 1.0 + Interval(3,5)

The first one works automatically.

To make the second line work I wrote this:

- Code: Select all
`def __radd__(self,other): #task 7`

f1, s1 = self.min, self.max

if isinstance(other, int): #w

f2, s2 = other, 1 #w

else:

f2, s2 = other.min, other.max

return Interval(f1+f2, s1+s2)

So the first and second line are fine.

But when I write

- Code: Select all
`print 1.0 + Interval(3,5)`

I get this error:

- Code: Select all
`Traceback (most recent call last):`

File "<stdin>", line 1, in <module>

File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile

execfile(filename, namespace)

File "C:\Users\mat11CJo\Downloads\Homework 2 Task 1, 2, 3, 4, 5, 6, 7, 8 N le.py", line 161, in <module>

print 1.0 + Interval(2,3)

File "C:\Users\mat11CJo\Downloads\Homework 2 Task 1, 2, 3, 4, 5, 6, 7, 8 N le.py", line 34, in __radd__

f2, s2 = other.min, other.max

AttributeError: 'float' object has no attribute 'min'

>>>

Line 160:

- Code: Select all
`print 1.0 + Interval(2,3)`

Line 34:

- Code: Select all
`f2, s2 = other.min, other.max`

in the __radd__ method.

Should I write float instead of int?

And I don't use f2, s2, so I realize now that they shouldn't be in __radd__.

Can someone help me please?

/ L.