Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Building a very simple vending machine in Python

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!!!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

  1. You are only taking input once, you need to take it in every iteration of the loop
  2. I think change owed should execute only when insert > coke
  3. 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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading