# Lab 2, Task 4 -- alternate solution # John B. Schneider # # Program to determine a new time given a current time and an offset # in minutes. This solution is a bit "cleaner" than the one described # in the lab. # # Obtain current time, but subtract one from the hour to have the # hours range between 0 and 11. print("What is the current time?") hour = int(input(" Hour: ")) - 1 minutes = int(input(" Minutes: ")) total_offset = int(input("Enter the offset [minutes]: ")) # Sum total offset to the current minutes and use divmod() to express # this in terms of hours and minutes. The minutes returned by this # will be the minutes for the new time. The hours will be an offset # from the current hour. hour_offset, new_minutes = divmod(total_offset + minutes, 60) # Obtain the new hour by adding in the offset, ensuring the value is # between 0 and 11, and then add back in the offset of 1 to obtain a # value between 1 and 12. new_hour = (hour + hour_offset) % 12 + 1 # Show the result. print("New time is:") print(" Hour:", new_hour) print(" Minutes:", new_minutes)