Noam
Noam

Reputation: 11

Overwrite __add__ method

class Clock:


   def __init__(self,hours,minutes):```

   self.hours=hours
   self.minutes=minutes



def Clock(hours,minutes):
        if hours>9 and minutes>9:
            return str(hours)+":"+str(minutes)
        elif hours>9 and minutes<10:
            return str(hours)+":0"+str(minutes)
        elif hours<10 and minutes>9:
            return "0"+str(hours)+":"+str(minutes)
        else:
            return "0"+str(hours)+":"+"0"+str(minutes)

def __add__(self,other):
    ????if Clock.Clock(self.hours,self.minutes)+Clock.Clock(other.hours,other.minutes)????



  t=Clock.Clock(5,6)+Clock.Clock(5,6)
  print(t)  

now when I print t I'm getting 05:0605:06 and I wanted to know how do I overwrite the __add__ function that when I'm doing Clock.Clock(self,other)+Clock.Clock(self,other) it's going to print out Clock(self,other)+Clock(self,other) which is= 10:12 for the input above.

Upvotes: 0

Views: 251

Answers (3)

Paul Rooney
Paul Rooney

Reputation: 21609

I have taken a few liberties with your code. First the Clock method looks like its trying to convert to a string. So instead lets define it as __str__. We can also implement the string conversion much more succinctly.

As for the __add__ method. To be consistent it should return a Clock object, not a string.

The other thing that the existing answers have missed, is to account for situations where the total minute value exceeds 59 in which case the excess should be included in the hours value.

To calculate this for minutes, take the modulus (%) 60 of total minutes. For hours divide by 60 and round down (or just do integer division). The function divmod conveniently does both these things for us in one function call.

class Clock:

    def __init__(self, hours, minutes):

        if minutes > 59:
            raise ValueError('invalid value for minutes %d' % minutes)

        self.hours = hours
        self.minutes = minutes

    def __str__(self):

        return "%d:%02d" % (self.hours, self.minutes)

    def __add__(self, other):

        extrahours, mins = divmod(self.minutes + other.minutes, 60)
        hours = self.hours + other.hours + extrahours
        return Clock(hours, mins)


t = Clock(5, 6) + Clock(5, 6)
print(t)

If we now try

t = Clock(5, 20) + Clock(5, 50)

It correctly prints 11:10 and not 10:70.

Upvotes: 1

Osman Mamun
Osman Mamun

Reputation: 2882

In [4]: class Clock:
   ...:
   ...:
   ...:    def __init__(self,hours,minutes):
   ...:        self.hours=hours
   ...:        self.minutes=minutes
   ...:
   ...:    def __repr__(self):
   ...:         if self.hours>9 and self.minutes>9:
   ...:             return str(self.hours)+":"+str(self.minutes)
   ...:         elif self.hours>9 and self.minutes<10:
   ...:             return str(self.hours)+":0"+str(self.minutes)
   ...:         elif self.hours<10 and self.minutes>9:
   ...:             return "0"+str(self.hours)+":"+str(self.minutes)
   ...:         else:
   ...:             return "0"+str(self.hours)+":"+"0"+str(self.minutes)
   ...:    def __add__(self, other):
   ...:        self.hours += other.hours
   ...:        self.minutes += other.minutes
   ...:        return Clock(self.hours, self.minutes)


In [5]: Clock(5, 6) + Clock(5,6)
Out[5]: 10:12

Upvotes: 1

xxMrPHDxx
xxMrPHDxx

Reputation: 667

You're adding string which would concatenate instead of actually adding the hour and minute values. You might want to do this instead

def __add__(self,other):
    return Clock.Clock(self.hours+other.hours,self.minutes+other.minutes)

Assuming the hours and minutes property is numeric

Upvotes: 2

Related Questions