OOP with MyTime

kjakupak at gmail.com kjakupak at gmail.com
Thu Jul 3 15:08:39 CEST 2014


On Thursday, July 3, 2014 9:01:09 AM UTC-4, Chris Angelico wrote:
> 
> 
> 
> And what happens when you run this code? A NameError, I would expect.
> 
> Do you understand how to define and call methods?
> 
> 
> 
> ChrisA

Altered the code. But yes a nameerror came up

class MyTime:

    def __init__(self, hrs=0, mins=0, secs=0):
        self.hours = hrs
        self.minutes = mins
        self.seconds = secs

        if self.seconds >= 60:
            self.minutes += self.seconds // 60
            self.seconds = self.seconds % 60

        if self.minutes >= 60:
            self.hours += self.minutes // 60
            self.minutes = self.minutes % 60

        if self.hours >= 24:
            self.hours = self.hours % 24

    def get_sec(self):
        return (self.hours * 60 + self.minutes) * 60 + self.seconds

    def __str__(self):
        return "{:02d}:{:02d}:{:02d}".\
               format(self.hours, self.minutes, self.seconds)

    def between(self, t1, t2):
        return (t1.get_sec() <= (self.get_sec()) and (self.get_sec()) <= (t2.get_sec())

s = MyTime()
t1 = s(9, 59, 59)
print("t1 =", t1)

t2 = s(10, 0, 1)
print("t2 =", t2)

t3 = s(10, 0, 0)
print("t3 =", t3)

print("between(t2, t3, t1) =", s.between(t2, t3, t1))
print("between(t1, t3, t2) =", s.between(t1, t3, t2))
print("between(t3, t1, t2) =", s.between(t3, t1, t2))



More information about the Python-list mailing list