I’m trying to build a vending machine with only one drink option that calculates the money inserted and memorises what was inserted and asks for the remaining money to be inserted…and if for example the money inserted still wasn’t enough continues to ask and update the remaining until the price of the drink is reached…
Im nearly there, but somehow need to add a bit of code so the program memorises and keeps updating as new values are inserted until reaches the drink price
Any help would be really appreciated!!!
here’s my code:
def main():
insert = int(input("Insert one coin at a time: ").strip())
coke = 50
total = coke - insert
while insert < coke:
print("Amount due: ", total)
return
if insert == coke:
print("Change Owed: ", total)
return
else:
print("Please insert the correct amount of: ", coke)
main()
>Solution :
There Were several errors:
- You are only taking input once, you need to take it in every iteration of the loop
- I think change owed should execute only when
insert > coke - You need to correct the 2nd condition since if
insert == coke, no change should be owed
Code to be used:
def main():
total = 0
while True:
total += int(input("Insert one coin at a time: ").strip())
coke = 50
print(total)
if total > coke:
print("Change Owed =", total - coke)
return
elif total == coke:
print("No Change Owed, Here's a coke ")
return
else:
print("Amount Due =", coke-total)
main()