Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Fix the coding erros: class Timer: def __init__(self, h, m): \'initialize the ti

ID: 3786044 • Letter: F

Question

Fix the coding erros:

class Timer:
def __init__(self, h, m):
'initialize the timer according to the hours and minutes passed as parameters'
self.hours = h
self.minutes = m

def tick( ):
'decrement the timer by one minute'
if self.minutes == 0:
self.minutes = 59
self.hours -= 1
else:
self.minutes -= 1

def zero(self):
'return True if the timer has reached 0:00'
self.hours == 0 and self.minutes == 0

def __str__(self):
return '{}:{:02d}'.format(h, m)

t = Timer(1,0)

# should print 1:00
print(t)

t.tick()

# should print 0:59
print(t)

for i in range(55):
t.tick()

# should print 0:04
print(t)

# This loop should print
# 0:03
# 0:02
# 0:01
# 0:00
while not t.zero():
t.tick()
print(t)

Explanation / Answer

class Timer:
   def __init__(self, h, m):
       self.h = h
       self.m = m
   def tick(self):
       if self.m == 0:
           self.m = 59
           self.h -= 1
       else:
           self.m -= 1
   def zero(self):
       return self.h == 0 and self.m == 0
      
   def __str__(self):
       return '{}:{:02d}'.format(self.h, self.m)

t = Timer(1,0)

print t

t.tick()

print t

for i in range(55):
   t.tick()
print t

while not t.zero():
   t.tick()
   print(t)