Classes: Exercise 2
Write a function called mul_time that takes a Time object and a number and returns a new Time object that contains the product of the original Time and the number.
Then use mul_time to write a function that takes a Time object that represents the finishing time in a race, and a number that represents the distance, and returns a Time object that represents the average pace (time per mile).
Then use mul_time to write a function that takes a Time object that represents the finishing time in a race, and a number that represents the distance, and returns a Time object that represents the average pace (time per mile).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Time: | |
def __init__(self, h=0, m=0, s=0): | |
self.hour = h | |
self.minute = m | |
self.second = s | |
def print_time(t1): | |
print("hour:minute:second = {h}:{m}:{s}" | |
.format(h=t1.hour, m=t1.minute, s=t1.second)) | |
def mul_time(t1, num): | |
t2 = Time(0,0,0) | |
timeInSec = (t1.hour*3600 + t1.minute*60 + t1.second)*num | |
t2.hour = int(timeInSec // 3600) | |
t2.minute = int((timeInSec - (t2.hour*3600))//60) | |
t2.second = int(timeInSec - (t2.hour*3600) - (t2.minute*60)) | |
return t2 | |
def ave_dist(time, dist): | |
return mul_time(time, 1/dist) |