# Lab 2, Task 4 # John B. Schneider # # Program to determine a new time given a current time and an offset # in minutes. This version follows the cookbook solution that is # described in the lab itself. Please also see the alternate solution # that provides a "cleaner" solution. # 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]: ")) # Express the offset in terms of hours and minutes. hour_offset, min_offset = divmod( total_offset, 60) # Obtain the new minutes -- but this may be more than 60. Will # correct that below. new_minutes = minutes + min_offset # Obtain the new hour. Use integer division to determine if there is # an additional hour contained within new_minutes. new_hour = hour + hour_offset + new_minutes // 60 # Convert the new minutes and hours to the proper format. Minutes # should be between 0 and 59 and hours between 1 and 12. new_minutes = new_minutes % 60 new_hour = new_hour % 12 + 1 # Show the result. print("New time is:") print(" Hour:", new_hour) print(" Minutes:", new_minutes)