I am a beginner coder struggling with a university assignment. Attached is the question and my code, I cannot figure out why it prints R20 instead of R10 when inputing: R30 for cost, R20 dep1, and R20 dep2.
Any help with refining the actual code would also be greatly appreciated, have turned to stack overflow as university is currently closed.
QUESTION:
Write a program called vending.py that simulates a vending machine with respect to calculating change based on the amount paid. The program will accept as input the cost of the item purchased, and will prompt the user to add money until the cost is met/exceeded. It will then calculate and print the change. Assume that payments and change are given in R50, R20, R10, R5, R2, and R1 amounts.
Sample IO:
Enter the cost (in Rand):
21
Deposit a coin or note (in Rand):
10
Deposit a coin or note (in Rand):
20
Your change is:
1 x R5
2 x R2
NOTE: The program will calculate the minimum number of coins to give while making change.
cost = 0
cost = eval(input("Enter the cost (in Rands): \n"))
dep1 = 0
dep1 = eval(input("Deposit a coin or note (in Rand): \n"))
while dep1 < cost:
dep2 = 0
dep2 = eval(input("Deposit a coin or note (in Rand): \n"))
total_dep = dep1 + dep2
total_dep = total_dep - cost
change = total_dep
if 100 >= change >= 50:
fifty = (change // 50)
print("Your change is: ", fifty, " x R50")
change -= fifty
if 50 > change >= 20:
twenty = (change // 20)
print("Your change is: ", twenty, " x R20")
change -= twenty
if 20 > change >= 10:
ten = (change // 10)
print("Your change is: ", ten, " x R10")
change -= ten
if 10 > change >= 5:
five = (change // 5)
print("Your change is: ", five, " x R5")
change -= five
if 5 > change >= 2:
two = (change // 2)
print("Your change is: ", two, " x R2")
change -= two
if 2 > change >= 1:
one = (change // 1)
print("Your change is: ", one, " x R1")
change -= one
print()
Aucun commentaire:
Enregistrer un commentaire