[Type C] Python Revision Tour-1 – Chapter 1
Table of Contents
11. Write a program that reads two times in military format and prints the number of hours and minutes between the two times.
A straightforward approach to the above problem would be easy but not helpful for students. It’s because you can’t always create complex programs the simple way. You would also learn new techniques and implementations if you just try to understand a few more lines of code. Still, for the sake of being Unit 1 in the book, here’s the simple approach to the program, followed by a systematic approach using functions and lastly, an alternative to the below code. Outputs are given for each code.
# Read the first time
first = int(input("Please Enter the first time (HHMM) = "))
# Read the second time
sec = int(input("Please Enter the second time (HHMM) = "))
# Calculate the difference
difference = sec - first
# Print the difference
print(difference // 100, "hours", difference % 100, "min")
Please Enter the first time = 830
Please Enter the second time = 1045
2 hours 15 min
Systematic Approach: The systematic approach is also better if you take interest in learning Object Oriented Programming in future or OOPS. It makes use of functions though classes are also needed for OOPS. But, it can help in making a solid foundation for unit 3 also.
def time_difference(time1, time2):
hours1, minutes1 = map(int, time1.split(':'))
hours2, minutes2 = map(int, time2.split(':'))
total_minutes1 = hours1 * 60 + minutes1
total_minutes2 = hours2 * 60 + minutes2
difference_minutes = abs(total_minutes2 - total_minutes1)
difference_hours = difference_minutes // 60
difference_minutes %= 60
return difference_hours, difference_minutes
# Example usage:
time1 = input("Enter the first time (HH:MM): ")
time2 = input("Enter the second time (HH:MM): ")
hours, minutes = time_difference(time1, time2)
print(f"Difference: {hours} hours and {minutes} minutes")
Enter the first time (HH:MM): 13:30
Enter the second time (HH:MM): 17:45
Difference: 4 hours and 15 minutes
The above approach towards the problem can be difficult to understand before learning the use of Functions towards unit 3. But, here’s a simple approach but number of lines of code enough to not provoke your teacher.
# Read the first time
time1 = input("Enter the first time (HH:MM): ")
hours1, minutes1 = int(time1[:2]), int(time1[3:])
# Read the second time
time2 = input("Enter the second time (HH:MM): ")
hours2, minutes2 = int(time2[:2]), int(time2[3:])
# Calculate the difference
total_minutes1 = hours1 * 60 + minutes1
total_minutes2 = hours2 * 60 + minutes2
difference_minutes = total_minutes2 - total_minutes1
difference_hours = difference_minutes // 60
difference_minutes %= 60
# Print the difference
print(f"Difference: {difference_hours} hours and {difference_minutes} minutes")
Enter the first time (HH:MM): 08:30
Enter the second time (HH:MM): 10:45
Difference: 2 hours and 15 minutes
This is the output you would see in the terminal after providing the input times 08:30
and 10:45
.