Hello:
I’m doing exercises from a book, Python for Everyone, but I found an error and I’d like to fix it.
The exercise is:
Write a programme that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times. The user inputs the times.
Please enter the first time: 0900
Please enter the second time: 1730
8 hours 30 minutes
Extra credit if you can deal with the case where the first time is later than the second:
Please enter the first time: 1730
Please enter the second time: 0900
15 hours 30 mintues
This is the solution given by the book:
##
# Compute the difference between two military times.
#
# Read inputs from the user.
time1 = int(input("Please enter the first time: "))
time2 = int(input("Please enter the second time: "))
# The following if statement handles the part of the work needed for the
# extra credit portion of this question.
if time2 < time1 :
time2 = time2 + 2360
# Compute the hours and minutes in the difference.
difference = time2 - time1
hours = difference // 100
minutes = difference % 100
# The following two statements handle the remainder of the work needed for
# extra credit by correcting any difference where the minutes value exceeds 60.
hours = hours + minutes // 60
minutes = minutes % 60
# Display the result.
print(hours, "hours", minutes, "minutes")
But if I choose: 1300 and 1250 it doesn’t result in 10 minutes, the programmes tells you it’s 50.
I’m not sure where to start, but I’d like to rewrite it in another way. I’m not asking you to write it, just an idea about how to approach the algorithm. I can then do it.
I posted about this before, but may be I was not clear enough. I’d like to delete the previous post, but I can’t.
Also, I’m not doing any courses or homework as you see the answer is with the code files, but this book is used at university level in the US and that’s why they mention credits. I’m just learning with the book in London and not through a course.
Thanks in advance.