Question: I am new to code and need to write the code for the function add_time2(time1, time2). I am unsure how to do this but some
I am new to code and need to write the code for the function add_time2(time1, time2). I am unsure how to do this but some ideas I had were: can I use add_time1 somewhere so it only modifies time1 or is this the wrong path since add_time1 returns somethings and for add_time2 we only need to modify time1 if it has changed? I have bolded the function and the script 'clock' that was already written. I'm just unsure how you would only modify time1 if time2 did not change. I really think youre supposed to use the function add_time1 but then this function modifies both time and time2 so maybe this is not the way to go?
import clock def add_time1(time1, time2): """ Returns the sum of time1 and time2 as a new Time object DO NOT ALTER time1 or time2, even though they are mutable Examples: The sum of 12hr 13min and 13hr 12min is 25hr 25min The sum of 1hr 59min and 3hr 2min is 4hr 1min Parameter time1: the starting time Precondition: time1 is a Time object Parameter time2: the time to add Precondition: time2 is a Time object """ sum_time_h = int(str(time1).split(':')[0]) + int(str(time2).split(':')[0]) sum_time_m = int(str(time1).split(':')[1]) + int(str(time2).split(':')[1]) if sum_time_m > 60: sum_time_m -=60 sum_time_h += 1 return clock.Time(sum_time_h,sum_time_m) def add_time2(time1, time2): """ Modifies time1 to be the sum of time1 and time2 DO NOT RETURN a new time object. Modify the object time1 instead. Examples: The sum of 12hr 13min and 13hr 12min is 25hr 25min The sum of 1hr 59min and 3hr 2min is 5hr 1min Parameter time1: the starting time Precondition: time1 is a Time object Parameter time2: the time to add Precondition: time2 is a Time object """ times = str(time1) times2 = str(time2)
code that was written already to use with (clock)
class Time(object): """ An instance represents a unit of time. Attributes: hours: Time in hours [int, must be nonnegative] minutes: Time in minutes [int in the rage 0..59] """ # Properties @property def hours(self): """ The number of hours in this time. **Invariant**: Value must be a positive int. """ return self._hours @hours.setter def hours(self, value): assert (type(value) == int), "value %s is not an int" % repr(value) assert (value >= 0), "value %s is not nonnegative" % repr(value) self._hours = value @property def minutes(self): """ The number of minutes in this time. **Invariant**: Value must be an int between 0 and 59, inclusive. """ return self._minutes @minutes.setter def minutes(self, value): assert (type(value) == int), "value %s is not an int" % repr(value) assert (value >= 0 and value <= 59), "value %s is outside of range [0,59]" % repr(value) self._minutes = value # Initializer def __init__(self,hours,minutes): """ **Constructor**: creates a new Time object with the given hours, minutes. Parameter hours: The number of hours Precondition: hours is a nonnegative int. Parameter minutes: The number of minutees Precondition: minutes is an int between 0 and 59, inclusive. """ self.hours = hours self.minutes = minutes def __eq__(self, other): """ Returns True if self and other are equivalent Time objects. Parameter other: The value to compare """ return (type(other) == Time and self.hours == other.hours and self.minutes == other.minutes) def __ne__(self, other): """ Returns True if self and other are not equivalent Time objects. Parameter other: The value to compare """ return not (self == other) def __str__(self): """ Returns a readable string representation of this time. """ min = str(self.minutes) if self.minutes < 10: min = '0'+min return str(self.hours)+":"+min def __repr__(self): """ Returns an unambiguous String representation of this time. """ return "%s(%s)" % (self.__class__,self.__str__())
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
