Calculating the difference between two times

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.

That’s the correct answer

time1 = 1300 
time2 = 1250 
// before so 
time2 = 1250 + 2360 = 3610 
3610 - 1300 = 2310 
hours = 38.5 
minutes = 50  

IMHO what is in need of changing is converting “military hours” into minutes.

I started the exercise at 12:50 and finished it at 13:00. The difference is ten minutes. First time 12:50, second time 1300. But gives me 50. That’s my thinking.

By the way, I don’t want to use modules yet if possible.

That way too.
1300 - 1250 = 50

With hours converted to minutes
780 - 770 = 10

1 Like

And how do I get the hour part of the number? Shall I put them into an array, and get the first two digits?

That’s a key part of the problem.

If time is expressed as “9:15 AM to 5:30 PM” splitting hours from minutes is easy using the colon (but you have the AM vs PM problem)

With military time there is no AM vs PM problem, but there is no delimiter to split on. eg. 915 to 1730

I guess there are a few approaches that could be taken to deal with the difference between the value having 3 or 4 digits

You could count the length and process slightly differently depending on whether it’s 3 or 4

You could take the last 2 digits as the minutes and what ever is left leading is the hours

Maybe a “time” function already exists that can do what you want? (sorry, I don’t know if one does)

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