def increment(time,seconds): t = Time() t.hour = time.hour + int(seconds/3600) t.minute = time.minute + int((seconds-int(seconds/3600))/60) t.second = time.second + (seconds % 60) return t
# Exercise 16-2. # Write a boolean function called is_after that takes two Time objects, t1 and t2, and # returns True if t1 follows t2 chronologically and False otherwise. Challenge: don’t use # an if statement. from time_class import Time def is_after(t1,t2): """ Returns True is t1 is later than t2 chronologically, else False. """ t1_in_seconds = 60*(60*t1.hour + t1.minute) + t1.second t2_in_seconds = 60*(60*t2.hour + t2.minute) + t2.second times_in_seconds = { t1_in_seconds: True, t2_in_seconds: False } return times_in_seconds[max(t1_in_seconds,t2_in_seconds)] t1 = Time() t2 = Time() t1.hour = t2.hour = 12 t1.minute = t2.minute = 10 t1.second = 1 t2.second = 2 print(is_after(t1,t2)) print(is_after(t2,t1))
# Exercise 16-1. # Write a function called print_time that takes a Time object and prints it in the form # hour:minute:second. Hint: the format sequence '%.2d' prints an integer using at least # two digits, including a leading zero if necessary. from time_class import Time def print_time(time): print("%.2d:%.2d:%.2d" % (time.hour,time.minute,time.second)) noon = Time() noon.hour = 12 noon.minute = 0 noon.second = 0 print_time(noon)