Start free

Advanced · Classes · 196 of 264

Clock arithmetic

Write a Time class holding hours and minutes, with a method that adds minutes and wraps around midnight. Print the new time as HH:MM.

Input: a time HH:MM, then a number of minutes to add (0 or more). Output: the new time.

Examples

Input
10:30
45
Output
11:15
Input
23:50
20
Output
00:10
Input
00:00
1440
Output
00:00

One more test checks other inputs when you press Check.

Stuck? The idea behind classes

A class describes a kind of object: its data (attributes, set in __init__) and what it can do (methods). Each object made from it has its own data.

self is the object a method is called on; __str__ says how the object prints.

class Account:
    def __init__(self, owner):
        self.owner = owner
        self.balance = 0

    def pay_in(self, amount):
        self.balance += amount

acc = Account("Ann")
acc.pay_in(5)
print(acc.balance)

Watch for: Every method's first parameter is self, and attributes are reached through it: self.balance, not balance.

All the classes exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More classes exercises