A Simple Python Code to Evaluate the End Time
If you're taking PCAP - Programming Essentials In Python , you may have encountered this question in 2.6.1.11 LAB: Operators and expressions asking you for a code to evaluate end time. It goes like this:
Scenario
Your task is to prepare a simple code able to evaluate the end time of a period of time, given as a number of minutes (it could be arbitrarily large). The start time is given as a pair of hours (0..23) and minutes (0..59). The result has to be printed to the console.For example, if an event starts at 12:17 and lasts 59 minutes, it will end at 13:16.
Don't worry about any imperfections in your code - it's okay if it accepts an invalid time - the most important thing is that the code produce valid results for valid input data.
Test your code carefully. Hint: using the % operator may be the key to success.
Solution with Comments:
hour = int(input("Starting time (hours): ")) #12dura = int(input("Event duration (minutes): ")) #59
The Code:
new_mins = mins + dura # Add a new variable for minutes to add the input minutes to the existing ones (mins = 17 . dura = 59. new_mins = 17+59 = 76)
new_hours = new_mins // 60 # Add a new variable for the new hours. // 60 means to get how many hours (rounded) in the new given minutes (76 // 60 = 1)
final_hours = new_hours + hour # Add a new variable to get the result needed for the hours. It's an addition process to add the input hour (12) + the new hours rounded (1) will be (13)
final_minutes = new_mins % 60 #Add a new variable to get the result needed for the minutes. It's an addition process to get the remainder of the new minutes (76) which is (16). Why 16? one hour = 60 minutes. 76 - 60 (this's called remainder %) = 16print(final_hours , ":" , final_minutes)
0 Comments
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.