Problem Analysis

On this day, Little Fish made precise timing of its swimming time and found that it swam from a time of b minutes to a time of c time of d minutes on the same day. Please help Little Fish calculate how long it swam in total that day.

The key to the problem lies in unifying units.

Unify units to minutes: a hours and b minutes is a * 60 + b minutes. c hours and d minutes is c * 60 + d minutes. The time difference is the difference between the two minutes. Then convert it to hours and minutes.

Conventional Approach

Step 1: Input Time

Use the input command to input the input time, separately input hours and minutes.

1
2
3
4
a = input('Please enter the starting hour (24-hour format):')
b = input('Please enter the starting minutes (24-hour format):')
c = input('Please enter the ending hour (24-hour format):')
d = input('Please enter the ending minutes (24-hour format):')

Step 2: Time Conversion

Convert hours to minutes by multiplying by 60 and adding the input minutes.

1
2
e = int(a) * 60 + int(b)
f = int(c) * 60 + int(d)

Step 3: Time Calculation

Subtract the starting time (minutes) from the ending time (minutes) to calculate the total duration.

1
g = f - e

Step 4: Convert to Hour Format

Divide the total minutes by 60 to get the integer part, and the remainder is the remaining minutes.

1
2
h = g // 60
i = g % 60

Step 5: Output

Remember to convert the string.

1
print('Total swimming time: ' + str(h) + ' hours ' + str(i) + ' minutes')

Code Example

1
2
3
4
5
6
7
8
9
10
a = input('Please enter the starting hour (24-hour format):')   #Waiting for input
b = input('Please enter the starting minutes (24-hour format):') #Waiting for input
c = input('Please enter the ending hour (24-hour format):') #Waiting for input
d = input('Please enter the ending minutes (24-hour format):') #Waiting for input
e = int(a) * 60 + int(b) #Convert the starting hours to minutes and add the starting minutes
f = int(c) * 60 + int(d) #Convert the ending hours to minutes and add the ending minutes
g = f - e #Total minutes subtraction to calculate swimming time
h = g // 60 #Integer division to get hours
i = g % 60 #Remainder to get minutes
print('Total swimming time: ' + str(h) + ' hours ' + str(i) + ' minutes') #String output

Partial Code Explanation

【//】 is integer division, 【%】 is the remainder

Integer division is to perform division, and only the integer part is retained. The remainder is what you learned in elementary school ↓

avatar

Borrowing Method (Not Recommended)

For example: The starting time is 6:30, and the ending time is 7:20. Normally, we can calculate the time in our heads as 50 minutes. In the computer, you need to split the hours and minutes to calculate, i.e., “ending hours - starting hours” “ending minutes - starting minutes”, here also involves a judgment condition, just like the example I gave, the ending minutes are smaller than the starting minutes, so the subtraction will result in a negative number, at this time we need to borrow 1 from the hours and then perform the operation.

Step 1: Input Time

Use the input command to input the input time, separately input hours and minutes.

1
2
3
4
a = input('Please enter the starting hour (24-hour format):')
b = input('Please enter the starting minutes (24-hour format):')
c = input('Please enter the ending hour (24-hour format):')
d = input('Please enter the ending minutes (24-hour format):')

Step 2: Minute Size Judgment

If the ending minutes are greater than the starting minutes, there is no need to borrow, otherwise you need to borrow. First use if to judge, if d < b i.e., the ending minutes are less than the starting minutes, then……

The code is in the next step.

Step 3: Borrowing Operation

In the hours, subtract the ending time from the starting time and subtract 1. In the minutes, because you have borrowed 1, add 60 (one hour is 60 minutes) to the ending minutes, and then subtract the ending minutes to avoid negative numbers.

Remember to output.

img

1
2
3
4
if d<b:
e = int(c) - int(a) - 1
f = (60 + int(b)) - int(d)
print('Total swimming time: ' + str(e) + ' hours ' + str(f) + ' minutes')

Step 4: Non-Borrowing Operation

If d is greater than b, i.e., the ending minutes are greater than the starting minutes, then direct subtraction can be used here. You need to use else, i.e., otherwise……

You also need to output:

1
2
3
4
else:
g = int(c) - int(a)
f = int(d) - int(b)
print('Total swimming time: ' + str(g) + ' hours ' + str(f) + ' minutes')

Code Example

1
2
3
4
5
6
7
8
9
10
11
12
a = input('Please enter the starting hour (24-hour format):')   #Waiting for input
b = input('Please enter the starting minutes (24-hour format):') #Waiting for input
c = input('Please enter the ending hour (24-hour format):') #Waiting for input
d = input('Please enter the ending minutes (24-hour format):') #Waiting for input
if d<b: #if for state judgment, if
e = int(c) - int(a) - 1 #operation process, convert input to integer
f = (60 + int(b)) - int(d) #operation process, convert input to integer
print('Total swimming time: ' + str(e) + ' hours ' + str(f) + ' minutes') #output result, convert number to string
else: #Included in the if judgment, otherwise
g = int(c) - int(a) #operation process, convert input to integer
f = int(d) - int(b) #operation process, convert input to integer
print('Total swimming time: ' + str(g) + ' hours ' + str(f) + ' minutes') #output result, convert number to string

Partial Code Explanation

if statement has not been learned yet, it will be learned later, and the general usage is something like this, detailed please see comments.