Calculating the difference between two times


I’m working with this book, that has this problem. But I test it with two times: 1250 and 1300 and it gives me the wrong answer: 0 hours 50 minutes. When it should say 10 minutes.

Write a programme that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times. You can deal with the case where the first time is later than the second. Also, what’s the 2360?

#  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
# the second 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
# the second part of the question 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")

Thanks in advance for your comments.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.