Implement the basic algorithm in Python to deepen your understanding of the algorithm. The second step is to deal with vending machine change calculations.
First, the vending machine mentioned here is defined. It can handle all Japanese banknotes and coins, and cannot be purchased when the input amount is insufficient. It is assumed that the user inputs the input amount and the purchase amount.
Create a vending machine function that simply calculates the change. We do not consider shortages. The code is as follows.
calc_change1.py
"""
2020/12/15
@Yuya Shimizu
vending machine
Change calculation ver.1
For the time being, calculate the change
"""
#Simple fishing calculation: fishing=Input amount-Purchase price
insert_price = input('Input amount>> ')
product_price = input('Purchase price>> ')
change = int(insert_price) - int(product_price)
print('Change>> {}Circle'.format(change))
Input amount>> 20000 #User input
Purchase price>> 1864 #User input
Change>>18136 yen#Display of calculation results
You can calculate the change, but how do you get the change back? It is preferable to return the change so that the number of banknotes and coins is as small as possible. In ver.2, change is returned with as few banknotes and coins as possible.
Create a vending machine function that returns change with as few banknotes and coins as possible. We do not consider shortages. The code is as follows.
calc_change2.py
"""
2020/12/15
@Yuya Shimizu
vending machine
Change calculation ver.2
Return change with as few banknotes as possible
"""
insert_price = input('Input amount>> ')
product_price = input('Purchase price>> ')
#Simple fishing calculation: fishing=Input amount-Purchase price
change = int(insert_price) - int(product_price)
total_change = change
#Return change with as few sheets as possible
cahnge_10000 = change//10000 #Number of 10,000 yen bills
change = change%10000
cahnge_5000 = change//5000 #Number of 5000 yen bills
change = change%5000
cahnge_1000 = change//1000 #Number of 1000 yen bills
change = change%1000
cahnge_500 = change//500 #Number of 500-yen coins
change = change%500
cahnge_100 = change//100 #Number of 100-yen coins
change = change%100
cahnge_50 = change//50 #Number of 50-yen coins
change = change%50
cahnge_10 = change//10 #Number of 10-yen coins
change = change%10
cahnge_5 = change//5 #Number of five-yen coins
change = change%5
cahnge_1 = change//1 #Number of 1-yen coins
print('Change{}Circle\n'.format(total_change))
print('10000 yen bill:{}Sheet\n5000 yen bill:{}Sheet\n1000 yen bill:{}Sheet\n500 yen ball:{}Sheet\n100 yen ball:{}Sheet\n50 yen ball:{}Sheet\n10 yen ball:{}Sheet\n5 yen ball:{}Sheet\n1 yen ball:{}Sheet\n'
.format(cahnge_10000, cahnge_5000, cahnge_1000, cahnge_500, cahnge_100, cahnge_50, cahnge_10, cahnge_5, cahnge_1))
Input amount>> 20000
Purchase price>> 1423
Change 18577 yen
10000 yen bill: 1 sheet
5000 yen bill: 1 sheet
1000 yen bill: 3 sheets
500 yen coin: 1 piece
100-yen coin: 0
50-yen coin: 1 piece
10-yen coin: 2 pieces
5 yen coin: 1 piece
1-yen coin: 2 pieces
I was able to return the change so that the number of banknotes and coins was as small as possible. Looking at the program, the calculation of the number of banknotes and coins is a repetition of the same thing. In ver.3, the same thing is repeated and the code is simplified.
Use a list to simplify the previous code. The code is as follows.
calc_change3.py
"""
2020/12/15
@Yuya Shimizu
vending machine
Change calculation ver.3
Simplify the code
"""
insert_price = input('Input amount>> ')
product_price = input('Purchase price>> ')
#Simple fishing calculation: fishing=Input amount-Purchase price
change = int(insert_price) - int(product_price)
total_change = change
#Banknotes Collect coins in a list and organize codes
money_list = [10000, 5000, 1000, 500, 100, 50, 10, 5, 1]
print('Change{}Circle\n'.format(total_change))
#Return change with as few sheets as possible
for money in money_list:
change_money = change//money
change = change%money
print('{}Yen bill:{}Sheet'.format(money, change_money))
Input amount>> 20000
Purchase price>> 1423
Change 18577 yen
10000 yen bill: 1 sheet
5000 yen bill: 1 sheet
1000 yen bill: 3 sheets
500 yen coin: 1 piece
100-yen coin: 0
50-yen coin: 1 piece
10-yen coin: 2 pieces
5 yen coin: 1 piece
1-yen coin: 2 pieces
The execution result did not change, and the code could be very short and simplified. Finally, in ver.4, we will create a vending machine function that responds to errors including shortages.
At the time of user input, it is assumed that the amount is correct, but you may mistakenly enter characters instead of numbers. In addition, the input amount may be insufficient for the purchase amount. In such a case, I want to send an error message and forcibly terminate the program.
Here, the method of the character string that can judge whether the character string `isdecimal ()`
is composed entirely of numbers is judged whether the user input information is correct, and `` `sys. Use exit () ```.
The code is as follows.
calc_change4.py
"""
2020/12/15
@Yuya Shimizu
vending machine
Change calculation ver.4
Respond to errors
"""
import sys
#Banknotes Collect coins in a list and organize codes
money_list = [10000, 5000, 1000, 500, 100, 50, 10, 5, 1]
insert_price = input('Input amount>> ')
#Forced termination if input is not an integer
if not insert_price.isdecimal(): #The string consists of only numbers(integer)Can be checked
print('Please enter an integer')
sys.exit() #Forced program termination
product_price = input('Purchase price>> ')
#Forced termination if input is not an integer
if not product_price.isdecimal():
print('Please enter an integer')
sys.exit()
#Simple fishing calculation: fishing=Input amount-Purchase price
change = int(insert_price) - int(product_price)
#Forced termination when change becomes negative
if change < 0:
print('Insufficient input amount')
sys.exit()
print('Change{}Circle\n'.format(change))
#Return change with as few sheets as possible
for money in money_list:
change_money = change//money
change %= money
print('{}Yen bill:{}Sheet'.format(money, change_money))
=========Execution result 1: When a non-number is entered in the input amount=========
Input amount>> 5.5
Please enter an integer
#forced termination
=========Execution result 2: When a non-number is entered in the purchase price=========
Input amount>> 5
Purchase price>>Ah
Please enter an integer
#forced termination
=========Execution result 3: When the input amount is insufficient=========
Input amount>> 5
Purchase price>> 150
Insufficient input amount
#forced termination
=========Execution result 4: In case of normal input=========
Input amount>> 500
Purchase price>> 150
Change 350 yen
10000 yen bill: 0 sheets
5000 yen bill: 0 sheets
1000 yen bill: 0 sheets
500 yen bill: 0 sheets
100-yen bill: 3
50-yen bill: 1
10-yen bill: 0
5 yen bill: 0 sheets
1 yen bill: 0 sheets
Execution results 1 to 3 show the output when an abnormal input or condition is met. As you can see from the result, it has been dealt with well. With this, we were able to create a vending machine function that responds to errors.
I knew the mechanism of the vending machine function to some extent, but it was a good opportunity to use the isdecimal and sys functions used in this program for the first time. Especially, I haven't used sys so much and I don't know much about it. I hope I can study in the future. In addition, I was able to reconfirm the simplification of the code by putting it together in a list again.
Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha
Recommended Posts