Pythondex
Python Programs

Vending Machine Python Program With Code

Last updated April 25, 2023 by Jarvis Silva

Want to know how to create a vending machine python program, then you are at the right place. Today in this tutorial I will show you step by step on how to create a simple vending machine program using python.

A vending machine is an automated machine which provides food items to the consumers. The consumer has to click the item they want and enter the money or cash into the machine to get the item.

We will create a similar vending machine program which will allow users to get the food item they want so let’s see how to do it.

Creating Vending Machine Python Program 

This program will be command line based and it will have no GUI. It will be a very simple and easy to use program.

Here’s how this vending machine program will work:

  • It will show user a list of food items along with their prices.
  • It will ask user to enter the code of the food that they want.
  • It will do user input and other validation.
  • It will ask the user to enter the price of the food they want.
  • Then it will give the user the food, just programmatically.

Above are some of the things about this program. I will show you step by step how to create this program, so let’s start.

1. Download and Install Python

Skip this step if you have done it, First you need to download python from the official website and after downloading run the setup it will install python on your computer.

To check if python is installed you can open command prompt and type python, it should not give any errors.

2. Project Setup

Now you have python installed, create a new folder for this program, open it in a code editor if you don’t have a code editor you can download and use vs code.

After opening the folder in vs code you can create a python file and go to the next step of copy and pasting the vending machine python code.

3. Vending Machine Python Code



items = [
    {
        'code':0,
        'name':'coca cola',
        'price':5
    },
    {
        'code':1,
        'name':'cadbury',
        'price':10
    },
    {
        'code':2,
        'name':'chips',
        'price':2
    },
]

is_quit = False
item = ''

while is_quit == False:
    print("Welcome to the vending machine")
    for i in items:
        print(f"Item Name: {i['name']} - Price: {i['price']} - code: {i['code']}")
    
    query = int(input("Enter the code number of the item you want to get: "))
    for i in items:
        if query == i['code']:
            item = i    
    if item == '':       
        print('INVALID CODE')
    else:    
        print(f"Great, {item['name']} will cost you {item['price']} dollars")        
    
        price = int(input(f"Enter {item['price']} dollars to purchase: "))
        if price == item['price']:
            print(f"Thank you for buying here is your {item['name']}")
        else:
            print(f"Please enter only {item['price']} dollars")    

    query = input("To quit the machine enter q and to continue buying enter anything: ")    
    if query == 'c':
        is_quit = False
    else:
        is_quit = True    
    print('')

Above is the python vending machine code, copy it and paste it in your python file. I have not used any libraries, so you don’t have to install any.

Now to run the program, open a command prompt or terminal at the project folder location and paste the below command to run the program or else you can use this online python compiler.


python filename.py

This will start the program and you should see a welcome to vending machine message. It will show you a list of food items, you can add more if you want.

It will ask you to enter the code number of the food item which is given, then it will ask you to enter the price of the item, then you will get the food item. Below is an example output of this program. 


Welcome to the vending machine
Item Name: coca cola - Price: 5 - code: 0
Item Name: cadbury - Price: 10 - code: 1
Item Name: chips - Price: 2 - code: 2
Enter the code number of the item you want to get: 3
INVALID CODE
Do you want to continue buying or quit to continue click c and to quit click q: c

Welcome to the vending machine
Item Name: coca cola - Price: 5 - code: 0
Item Name: cadbury - Price: 10 - code: 1
Item Name: chips - Price: 2 - code: 2
Enter the code number of the item you want to get: 2
Great, chips will cost you 2 dollars
Enter 2 dollars to purchase: 2
Thank you for buying here is your chips
Do you want to continue buying or quit to continue click c and to quit click q:

As you can see, we have successfully created a vending machine program and it works similar to a vending machine.

Python Vending Machine Program Explanation

Now let’s see and understand how each line of this program works. If you are an experienced programmer then you don’t have to read this, but if you are a beginner then read this part.



items = [
    {
        'code':0,
        'name':'coca cola',
        'price':5
    },
    {
        'code':1,
        'name':'cadbury',
        'price':10
    },
    {
        'code':2,
        'name':'chips',
        'price':2
    },
]

First we have the items list which has nested python dictionaries. As you can see, each item has 3 keys: name, code and price. You can add more food items to this list if you want but remember to add the code value linearly.


while is_quit == False:
  # code

Then we have the while loop which runs till the variable is_quit becomes true, this allows the program to run as long as the user wants to quit.


print("Welcome to the vending machine")
for i in items:
    print(f"Item Name: {i['name']} - Price: {i['price']} - code: {i['code']}")

Now inside the while loop we have a print statement which welcomes and a for loop which iterates over the food items and prints the name, code and price in order format.


    query = int(input("Enter the code number of the item you want to get: "))
    for i in items:
        if query == i['code']:
            item = i    
    if item == '':       
        print('INVALID CODE')
    else:    
        print(f"Great, {item['name']} will cost you {item['price']} dollars")

Now this line of code asks the user to enter the code number of the food item they want, after that it finds the food item with the code number and set’s the food item into an item variable.

If they enter any wrong or invalid code number, then it prints an INVALID CODE. If it enters correctly and matches the food item, then it goes in the else statement.


    else:    
        print(f"Great, {item['name']} will cost you {item['price']} dollars")        
    
        price = int(input(f"Enter {item['price']} dollars to purchase: "))
        if price == item['price']:
            print(f"Thank you for buying here is your {item['name']}")
        else:
            print(f"Please enter only {item['price']} dollars")    

Inside else statement it prints the food details they want with price then it asks the user to enter the price amount to get the food and if it matches the price of the food they want they get the food, if not then they don’t get.


    query = input("To quit the machine enter q and to continue buying enter anything: ")    
    if query == 'c':
        is_quit = False
    else:
        is_quit = True    
    print('')

Lastly it asks the user if they want to continue or quit the program, so to continue they can enter anything except q because that is for quitting.

Summary

This was the tutorial on creating a vending machine program in python. I hope you found this program useful and helpful. Do share this with your friends who might be interested in this program.

Here are more python programs you might find interesting:

I hope you found what you were looking for from this python tutorial and if you want more python tutorials like this then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂