Question 1: Buying Stationery

Buying Stationery: Given that the cost of the stationery to be purchased is 2 yuan and 3 jiao. The total amount of money available for purchasing stationery is a yuan and b jiao. Calculate how many pieces of stationery can be bought in total?

1. Problem Analysis

To buy stationery, we need to determine how many pieces can be bought. This involves elementary division. For example, if I have 100 yuan, and each piece of stationery costs 2.3 yuan, I can buy 100 / 2.3 pieces.

2. Data Input

This question only requires inputting a numerical value, which is the amount of money you have brought. Use input for the input.

1
a = input('How much money did you bring: ')

3. Data Calculation

The problem states that each piece of stationery costs 2.3 yuan, so we need to divide the input value by 2.3.

1
b = (float(a) / 2.3)

4. Data Output

Present the final result to find the answer to this question.

1
print(b)

Code Example

1
2
3
4
5
a = input("Please enter the amount in yuan: ")  # Wait for input
b = (float(a) / 2.3) # Price calculation, assign a as a floating point
print('You can buy a total of ' + str(b) + ' pieces of stationery') # Output the result, convert b to a string
d = int(b) # Convert b to an integer
print('Equivalent to ' + str(d) + ' pieces') # Output the result, convert b to a string

Partial Code Explanation

Spaces can be added before and after operators in expressions.

In the calculation, a can accept a floating-point number, i.e., “You brought 10.3 yuan.”

Use int because the calculation result may have a decimal part, and an adjustment is made at this step.

In the output result, if there are text elements, they need to be enclosed in quotes, and when concatenating numbers, it is necessary to forcibly convert the numbers to strings.

Question 2: Little Fish Swimming

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

Problem Analysis

This question is more complex. The time format is 00:00 and cannot be calculated directly. It needs to be split and calculated.

For example: The start time is 6:30, and the end time is 7:20. Normally, we would mentally calculate the time as 50 minutes. In a computer, hours and minutes need to be split and calculated, i.e., “end hour - start hour” and “end minute - start minute.” This also involves a conditional judgment. For example, in the example I provided, the end minute is smaller than the start minute, so the subtraction will result in a negative number. In this case, we need to subtract the start minute from 60 and then add the end minute. Additionally, we need to borrow 1 from the hour.

For a detailed explanation of the problem, see Little Fish Swimming Problem Explanation.

Code Example

1
2
3
4
5
6
7
8
9
10
11
12
a = input('Please enter the start hour (24-hour format): ')  # Wait for input
b = input('Please enter the start minute (24-hour format): ') # Wait for input
c = input('Please enter the end hour (24-hour format): ') # Wait for input
d = input('Please enter the end minute (24-hour format): ') # Wait for input
if d < b: # if for state judgment, if
e = int(c) - int(a) - 1 # calculation process, convert input to integer
f = (60 + int(b)) - int(d) # calculation process, convert input to integer
print('Total swimming time: ' + str(e) + ' hours ' + str(f) + ' minutes') # Output the result, convert numbers to strings
else: # included in the if judgment, otherwise
g = int(c) - int(a) # calculation process, convert input to integer
f = int(d) - int(b) # calculation process, convert input to integer
print('Total swimming time: ' + str(g) + ' hours ' + str(f) + ' minutes') # Output the result, convert numbers to strings

Partial Code Explanation

if statements have not been learned yet, but they will be learned in the future. The general usage is something like this, with detailed explanations in the comments.