Timer Management

Create a class Timer with private attributes for minutes and seconds. Implement a method to increment the time, ensuring that the time stays within 24 hours.

Example 1:

Input: Increment Time: 30 minutes
Output: "Time updated"

Example 2:

Input: Increment Time: 25 hours
Output: "Invalid increment"

Update minutes and convert to hours if minutes exceed 60. Check if the total time exceeds 24 hours.

class Timer:
    def __init__(self):
        self._hours = 0
        self._minutes = 0

    def increment(self, hours=0, minutes=0):
        self._hours += hours
        self._minutes += minutes

        self._hours += self._minutes // 60
        self._minutes %= 60

        if self._hours >= 24:
            self._hours = self._minutes = 0
            return "Invalid increment"
        else:
            return "Time updated"

    def get_time(self):
        return f"{self._hours} hours, {self._minutes} minutes"

# Test the class
timer = Timer()
timer.increment(minutes=30)  # Output: Time updated
timer.increment(hours=25)  # Output: Invalid increment

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!