Define a class clock in python for advancing time. The description is as follow: -hr(hour) = 0.0 -mint(minutes) = 0.0 -sec(seconds) = 0.0 The following methods need to be described: -set_time() : It is a fuction to input the correct time from the user -advance_time() : It is a function to input the time by which the original time is to be increased from the user and hence advances the time. -show_time(): It is a function to print the advanced time.
In [3]:
class clock(object):
'''Class to create a clock object with the following attributes:-
hr : Hours (float). Default value 0.0
mint : Minutes (float) Default value 0.0
sec : Seconds (float) Default value 0.0
The following methods are available:
set_time(): Prompts the user to input initial time
advance_time(hour, minute, second): Advances the time by the given number of seconds, minutes, or hours.
show_time(): Prints the advanced time'''
def __init__(self):
self.hr = 0.0
self.mint = 0.0
self.sec = 0.0
def set_time(self):
while True:
hour = float(raw_input('Enter hour: '))
if hour>12:
print 'Invalid entry. Hour must be less than 12'
else:
break
while True:
minute = float(raw_input('Enter minutes: '))
if minute>59:
print 'Invalid entry. Minutes must be less than 60'
else:
break
while True:
second = float(raw_input('Enter seconds: '))
if second>59:
print 'Invalid entry. Seconds must be less than 60'
else:
break
self.hr += hour
self.mint += minute
self.sec += second
def advance_time(self,hour = 0,minute = 0,second = 0):
if self.hr + hour<12:
self.hr+=hour
else:
self.hr = (self.hr + hour)%12
if self.mint + minute<60:
self.mint+=minute
else:
self.mint = (self.mint + minute)%60
self.hr+=self.mint/60
if self.sec + second<60:
self.sec += second
else:
self.sec = (self.sec + second)%60
self.mint += self.sec/60
def show_time(self):
print '%d:%d:%d'%(self.hr,self.mint,self.sec)