class - Python Classes did I make a mistake or did my teacher [easy] -


my teacher told make clock class take hours, minutes , seconds instance variables. gave bunch of different methods had make complete objectives gave regarding clock class.

one of our tasks implement method called print12() prints out time colons , appends or pm end. asked test entering print12(myclock). mine works if enter myclock.print12(). i'm not sure if made error or implemented code wrong.

here code:

class clock:     def __init__(self, hour, minute, second):         self.hour = hour         self.minute = minute         self.second = second     def __str__(self):         return '%02d'%self.hour+':'+'%02d'%self.minute+':'+'%02d'%self.second     def print12(self):         if self.hour >= 13:             print(self,"p.m.")         else:             print(self,"a.m.")     def advance(self,s1):         if s1 == 'sec':             self.second += 1         elif s1 == 'min':             self.minute += 1         elif s1 == 'hour':             self.hour += 1         if self.second == 60:             self.second = 0             self.minute += 1         if self.minute == 60:             self.minute = 0             self.hour += 1         if self.hour == 24:             self.hour = 0             self.minute = 0             self.second = 0 

here main code test program:

myclock = clock(15,59,5) print(myclock) print12(myclock) myclock.advance('sec') print(myclock) myclock.advance('min') print(myclock) 

this not work me work if replace print12(myclock) myclock.print12().

based solely on reporting, teacher meant 1 of 2 things:

1) create method of clock prints indicated:

class clock:     ...     def print12(self):         if self.hour >= 13:             print(self,"p.m.")         else:             print(self,"a.m.")   # usage:  myclock=clock(...)  myclock.print12() 

2) create function, outside of clock, takes clock argument , prints indicated:

class clock:     ...  def print12(clock):     if clock.hour >= 13:         print(clock,"p.m.")     else:         print(clock,"a.m.")  # usage: myclock = clock(...) print12(myclock) 

you'll need ask instructor meant.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -