Time

Report a typo

We have the Time class. We have defined two attributes: the hour and the minute in its initializer.

class Time:

    def __init__(self, hour, minute):
        self.hour = hour
        self.minute = minute

To create class instances from such strings as 14:00, we need to parse them as two string variables. Write a from_string method that will serve as an alternative constructor for the Time class. Use the split(":") to divide the given string.

For example, given the input 14:05, we can create the time instance with the following attribute values:

print(time.hour)    # 14
print(time.minute)  # 05

You do not need to take any input or create new class instances, just write the body of the method and specify the correct decorator.

Write a program in Python 3
class Time:

def __init__(self, hour, minute):
self.hour = hour
self.minute = minute

# use appropriate decorator
def from_string(...):
...
___

Create a free account to access the full topic