Welcome to python. In python everything is easy. Firstly you need to understand your problem on a slightly deeper level though. The problem here is just realizing that you’re taking two slightly different sums. A good way to go about this would be to represent them both in a common form (minutes) then do all of your math there and change back only for display purposes. For example:
def time_diference(time1, time2):
hour_change = abs(int(time1[:2]) - int(time2[:2]))
minute_change = int(time2[2:]) - int(time1[2:])
# Change hours to equivalent minutes so you're really comparing apples to apples
total_minute_change = hour_change * 60 + minute_change
return total_minutes_change
def print_time(minute_difference):
# Display the answer back to the user
print_time(time_difference(first, second))
Now that you’ve got them compared and you have a difference you should have a better grasp of how to proceed, but there are still edge cases to think about.
For instance, what happens if you’ve got the times “0900” and “0845”. In your original example you might want to think of another edge case, which is the case when you’ve got negative minutes, such as: 0945
and 1337
, in which case your solution breaks.
I’m trying hard not to spoonfeed you since this is the kind of stuff you really need to figure out largely on your own and since this is homework, but I hope this gives you a good place to start.
1
solved Python for beginners [closed]