• About
  • Advertise
  • Privacy & Policy
  • Contact
Sunday, January 11, 2026
  • Login
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
    • Home – Layout 6
  • News
    • All
    • Business
    • Politics
    • Science
    • World
    Hillary Clinton in white pantsuit for Trump inauguration

    Hillary Clinton in white pantsuit for Trump inauguration

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    Trending Tags

    • Trump Inauguration
    • United Stated
    • White House
    • Market Stories
    • Election Results
  • Tech
    • All
    • Apps
    • Gadget
    • Mobile
    • Startup
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
    • All
    • Gaming
    • Movie
    • Music
    • Sports
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    So you want to be a startup investor? Here are things you should know

    So you want to be a startup investor? Here are things you should know

  • Lifestyle
    • All
    • Fashion
    • Food
    • Health
    • Travel
    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    How couples can solve lighting disagreements for good

    How couples can solve lighting disagreements for good

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Trending Tags

    • Golden Globes
    • Game of Thrones
    • MotoGP 2017
    • eSports
    • Fashion Week
  • Review
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    Intel Core i7-7700K ‘Kaby Lake’ review

    Intel Core i7-7700K ‘Kaby Lake’ review

No Result
View All Result
Ai News
Advertisement
  • Home
    • Home – Layout 1
    • Home – Layout 2
    • Home – Layout 3
    • Home – Layout 4
    • Home – Layout 5
    • Home – Layout 6
  • News
    • All
    • Business
    • Politics
    • Science
    • World
    Hillary Clinton in white pantsuit for Trump inauguration

    Hillary Clinton in white pantsuit for Trump inauguration

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Amazon has 143 billion reasons to keep adding more perks to Prime

    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    Trending Tags

    • Trump Inauguration
    • United Stated
    • White House
    • Market Stories
    • Election Results
  • Tech
    • All
    • Apps
    • Gadget
    • Mobile
    • Startup
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    These Are the 5 Big Tech Stories to Watch in 2017

    These Are the 5 Big Tech Stories to Watch in 2017

    Trending Tags

    • Nintendo Switch
    • CES 2017
    • Playstation 4 Pro
    • Mark Zuckerberg
  • Entertainment
    • All
    • Gaming
    • Movie
    • Music
    • Sports
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    Harnessing the power of VR with Power Rangers and Snapdragon 835

    So you want to be a startup investor? Here are things you should know

    So you want to be a startup investor? Here are things you should know

  • Lifestyle
    • All
    • Fashion
    • Food
    • Health
    • Travel
    Shooting More than 40 Years of New York’s Halloween Parade

    Shooting More than 40 Years of New York’s Halloween Parade

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Heroes of the Storm Global Championship 2017 starts tomorrow, here’s what you need to know

    Why Millennials Need to Save Twice as Much as Boomers Did

    Why Millennials Need to Save Twice as Much as Boomers Did

    Doctors take inspiration from online dating to build organ transplant AI

    Doctors take inspiration from online dating to build organ transplant AI

    How couples can solve lighting disagreements for good

    How couples can solve lighting disagreements for good

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Ducati launch: Lorenzo and Dovizioso’s Desmosedici

    Trending Tags

    • Golden Globes
    • Game of Thrones
    • MotoGP 2017
    • eSports
    • Fashion Week
  • Review
    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    The Legend of Zelda: Breath of the Wild gameplay on the Nintendo Switch

    Shadow Tactics: Blades of the Shogun Review

    Shadow Tactics: Blades of the Shogun Review

    macOS Sierra review: Mac users get a modest update this year

    macOS Sierra review: Mac users get a modest update this year

    Hands on: Samsung Galaxy A5 2017 review

    Hands on: Samsung Galaxy A5 2017 review

    The Last Guardian Playstation 4 Game review

    The Last Guardian Playstation 4 Game review

    Intel Core i7-7700K ‘Kaby Lake’ review

    Intel Core i7-7700K ‘Kaby Lake’ review

No Result
View All Result
Ai News
No Result
View All Result
Home Machine Learning

Implementing the Coffee Machine in Python

AiNEWS2025 by AiNEWS2025
2025-09-08
in Machine Learning
0
Implementing the Coffee Machine in Python
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


of the easiest programming languages, and one that encapsulates within itself diversity and the potential to include code as complex as they come. While there are a number of projects and tutorials on beginner-friendly Python-based coding, in this article, we will learn to build a Coffee Machine program in Python. This article will provide an understanding and practice of conditional statements, loops, and Python dictionaries and will serve as a basis for complex coding (in a later article).

Understanding the Project Requirements

First things first, let us try to understand how the program will work, what the basic requirements are, what variables and conditionals we will need, and the scope of the Coffee Making Machine.

The program will function as follows: The program will display menu items and ask the user if they want a drink. The user may choose a drink of their liking.

If the user chooses a drink, we will have to make sure our coffee machine has enough resources to make the coffee. If it has, then we will continue ahead. Otherwise, we will let the user know.

If the resources are enough, then we will ask the user for payment in the form of coins of nickels, pennies, dimes, and quarters. We will calculate the payment made versus the cost. If the payment is complete, we will make the coffee and serve it. If the payment made is more than the price of the coffee, we will give them their change back. Otherwise, if the payment falls short of the price, we will reject the order and give back their coins.

An exception we will add is that if someone from management wants to know the resources left, or the money the machine has stored after completing orders, they may access this by typing ‘report’. Moreover, if the management wants to switch off the machine completely, they may do so by typing ‘off’

Let us define all this with the help of a flowchart:

Flowchart (Image by Author)

Step 1: Defining Menu & Resources

The first step in coding this Coffee Machine Project is to define the menu as well as the resources the machine has to prepare any order. In our example, we will have 3 items on the menu: Latte, Espresso, and Cappuccino.

We will use the Python dictionary in order to define the menu variable. A Python dictionary is a useful data type that stores data against a key, both of which are easily accessible. The menu variable will be a dictionary that not only stores the 3 menu items, ie, latte, cappuccino, and espresso, but also describes the ingredients that are required to make them and their price. Let us code the above:

menu = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "milk" : 0,
            "coffee": 20,
        },
        "price": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 150,
            "milk": 200,
            "coffee": 25,
        },
        "price": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 25,
        },
        "price": 3.0,
    }
}

The next task is to define the resources we have. These are the resources that would be required to make the different types of coffee, as well as the money that is generated by selling the coffee.

resources = {
    "water": 1000,
    "milk": 1000,
    "coffee": 100,
    "money": 0
}

As you can see, we have specific amounts of each ingredient as our resources, and 0 money as no coffee has been sold initially.

Step 2: Ask User for Order

The next step is to ask the user what they would like to order. We will use the Python input function for this purpose. Moreover, we will convert the input string to lowercase so it will be easily matched in our conditionals that will follow.

order = input("What would you like to order?\n Cappuccino, Latte or Espresso?\n").lower()

Step 3: Add Special Cases using Conditionals

Next, we will add 2 special cases. The first one is that if the management wants to turn the machine completely off, they will enter off as input to the above statement, and the machine will turn off, or in other words, the program will end. We will define a variable for this purpose called end that will be False initially and will turn True when the management wants to turn off the machine.

Another case we will add here is when the management would like to know the resources in the coffee machine, they will enter report, and the machine will print a report of the available resources. Let us put these cases to code:

if order == 'off':
    end = True
elif order == 'report':
    print(f"We have the following resources:\nWater: {resources['water']}ml\nMilk: {resources['milk']}ml\nCoffee: {resources['coffee']}g \nMoney: ${resources['money']}")

Note that we have used \n and f-string to properly format the report. Result would be:

Report (Image by Author)

Step 4: Check Resources

The next step is to check the resources required to make coffee. If an espresso requires 50ml of water and 20g of coffee, and either of the ingredients is insufficient, we cannot proceed with making coffee. Only when the ingredients are there will we proceed towards making coffee.

This is going to be lengthy to code, we will check one by one whether each of the ingredients are there in our resources:

if resources['water'] >= menu[order]['ingredients']['water']:
    if resources['milk'] >= menu[order]['ingredients']['milk']:
        if resources['coffee'] >= menu[order]['ingredients']['coffee']:
            #We will prepare order
        else:
            print("\nResources insufficient! There is not enough coffee!\n")
    else:
        print("\nResources insufficient! There is not enough milk!\n")
else:
    print("\nResources insufficient! There is not enough water!\n")

Only when the 3 conditions are checked, then we will proceed with making the coffee; otherwise, print to the user which of the ingredients is not sufficient. Also, we have to make sure that the user has paid the price of their order. Let us do that.

Step 5: Ask and Calculate Payment

For the condition listed above, where all resources are available, the next step is to ask the user to pay the price. Once the payment is done, we will make the coffee.

print(f"Pay ${menu[order]['price']}\n")

Now we will ask the user to insert coins (our coffee machine is outdated, so bear with the clinking of the coins :P). We will ask for the coins they are inserting and calculate the total, then compare it with the price of their order.

(Photo by Erik Mclean on Unsplash)

Coins inserted in the coffee machine are of 4 different types:

  • Pennies = $0.01
  • Nickels = $0.05
  • Dimes = $0.10
  • Quarters = $0.25

The total value will be calculated by multiplying the number of coin types by their values. Make sure to convert the input into an int type; otherwise, you will have an error.

print("Insert coins")
                p = int(input("Insert pennies"))
                n = int(input("Insert nickels"))
                d = int(input("Insert dimes"))
                q = int(input("Insert quarters"))
                total = (p * 0.01) + (n * 0.05) + (d * 0.10) + (q * 0.25)

Next, is to check whether the total amount calculated is equal to the price of the coffee selected or not? Here is how we will code it:

if total == menu[order]['price']:
    print("Transaction successful. Here is your coffee!")
elif total > menu[order]['price']:
    change = total - menu[order]['price']
print(f"You have inserted extra coins. Here is your change ${change}\n")
    else:
print("Payment not complete. Cannot process order")

Only when the amount is equal to the price of the coffee, is the transaction successful. If the total value is greater than the price, we will need to return the change to the user. Otherwise, if the total value falls short of the price, we will cancel the order.

Step 6: Make Coffee

The last step is to dispense the coffee to the user, and in our coding world, we will do so by updating both the ingredients and the money in our resources dictionary.

if total == menu[order]['price']:
    resources['water'] = resources['water'] - menu[order]['ingredients']['water']
    resources['coffee'] = resources['coffee'] - menu[order]['ingredients']['coffee']
    resources['milk'] = resources['milk'] - menu[order]['ingredients']['milk']
    resources['money'] = resources['money'] + menu[order]['price']
    print("Transaction successful. Here is your coffee!")

Notice that the ingredients are subtracted from the resources while money is added. Hence, our resources dictionary is updated with each order delivered. The same condition will also be added when we have an additional amount and are required to give change back.

Step 7: Program Continuity

After the coffee order is processed, as long as the management doesn’t command the coffee machine to switch off, the machine will keep on asking the user for their coffee order, processing payments, updating resources, and dispensing the coffee. So we will include a while loop to ensure the program continues after the drink has been dispensed.

The entire block of code we have coded above will be included in this while loop:

while end != True:
    order = input("\nWhat would you like to order?\nCappuccino, Latte or Espresso?\n").lower()

    if order == 'off':
        end = True
    elif order == 'report':
        print(f"We have the following resources:\nWater: {resources['water']}ml\nMilk: {resources['milk']}ml\nCoffee: {resources['coffee']}g \nMoney: ${resources['money']}")

    elif resources['water'] >= menu[order]['ingredients']['water']:
        if resources['milk'] >= menu[order]['ingredients']['milk']:
            if resources['coffee'] >= menu[order]['ingredients']['coffee']:
                    print(f"Pay ${menu[order]['price']}\n")

                    # TODO : Coins insert
                    print("Insert coins")
                    p = int(input("Insert pennies"))
                    n = int(input("Insert nickels"))
                    d = int(input("Insert dimes"))
                    q = int(input("Insert quarters"))
                    total = (p * 0.01) + (n * 0.05) + (d * 0.10) + (q * 0.25)

                    if total == menu[order]['price']:
                        resources['water'] = resources['water'] - menu[order]['ingredients']['water']
                        resources['coffee'] = resources['coffee'] - menu[order]['ingredients']['coffee']
                        resources['milk'] = resources['milk'] - menu[order]['ingredients']['milk']
                        resources['money'] = resources['money'] + menu[order]['price']
                        print("Transaction successful. Here is your coffee!")
                    elif total > menu[order]['price']:
                        change = total - menu[order]['price']
                        print(f"You have inserted extra coins. Here is your change ${change}\n")
                    else:
                        print("Payment not complete. Cannot process order")

            else:
                print("\nResources insufficient! There is not enough coffee!\n")
        else:
            print("\nResources insufficient! There is not enough milk!\n")
    else:
        print("\nResources insufficient! There is not enough water!\n")

Conclusion

We have successfully converted the working of a coffee machine into Python code. Over the course of this project, we explored dictionaries and accessing them, conditional statements, and the while loop. While quite straightforward, we can further simplify this project with other techniques such as Object Oriented Programming (a project for next time). Do share your feedback regarding this project and any suggestions on how we can further improve it. Till then, enjoy your coffee!

(Photo by Nathan Dumlao on Unsplash)

Access the full code here: https://github.com/MahnoorJaved98/Coffee-Machine/blob/main/Coffee%20Machine%20-%20Python.py

Source link

#Implementing #Coffee #Machine #Python

Tags: codingdata scienceEditors PickProgrammingPython
Previous Post

Tiny Vinyl is a new pocketable record format for the Spotify age

Next Post

Why basic science deserves our boldest investment

AiNEWS2025

AiNEWS2025

Next Post
Why basic science deserves our boldest investment

Why basic science deserves our boldest investment

Stay Connected test

  • 23.9k Followers
  • 99 Subscribers
  • Trending
  • Comments
  • Latest
A tiny new open source AI model performs as well as powerful big ones

A tiny new open source AI model performs as well as powerful big ones

0
Water Cooler Small Talk: The Birthday Paradox 🎂🎉 | by Maria Mouschoutzi, PhD | Sep, 2024

Water Cooler Small Talk: The Birthday Paradox 🎂🎉 | by Maria Mouschoutzi, PhD | Sep, 2024

0
Ghost of Yōtei: The acclaimed Ghost of Tsushima is getting a sequel

Ghost of Yōtei: The acclaimed Ghost of Tsushima is getting a sequel

0
Best Headphones for Working Out (2024): Bose, Shokz, JLab

Best Headphones for Working Out (2024): Bose, Shokz, JLab

0
Can One AI Platform Replace Your Creative Tool Stack?

Can One AI Platform Replace Your Creative Tool Stack?

2026-01-10
Federated Learning, Part 1: The Basics of Training Models Where the Data Lives

Federated Learning, Part 1: The Basics of Training Models Where the Data Lives

2026-01-10
Conservative lawmakers want porn taxes. Critics say they’re unconstitutional.

Conservative lawmakers want porn taxes. Critics say they’re unconstitutional.

2026-01-10
Elon Musk says he’s going to open-source the new X algorithm next week

Elon Musk says he’s going to open-source the new X algorithm next week

2026-01-10

Recent News

Can One AI Platform Replace Your Creative Tool Stack?

Can One AI Platform Replace Your Creative Tool Stack?

2026-01-10
Federated Learning, Part 1: The Basics of Training Models Where the Data Lives

Federated Learning, Part 1: The Basics of Training Models Where the Data Lives

2026-01-10
Conservative lawmakers want porn taxes. Critics say they’re unconstitutional.

Conservative lawmakers want porn taxes. Critics say they’re unconstitutional.

2026-01-10
Elon Musk says he’s going to open-source the new X algorithm next week

Elon Musk says he’s going to open-source the new X algorithm next week

2026-01-10
Footer logo

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Follow Us

Browse by Category

  • AI & Cloud Computing
  • AI & Cybersecurity
  • AI & Sentiment Analysis
  • AI Applications
  • AI Ethics
  • AI Future Predictions
  • AI in Education
  • AI in Fintech
  • AI in Gaming
  • AI in Healthcare
  • AI in Startups
  • AI Innovations
  • AI News
  • AI Research
  • AI Tools & Automation
  • Apps
  • AR/VR & AI
  • Business
  • Deep Learning
  • Emerging Technologies
  • Entertainment
  • Fashion
  • Food
  • Gadget
  • Gaming
  • Health
  • Lifestyle
  • Machine Learning
  • Mobile
  • Movie
  • Music
  • News
  • Politics
  • Review
  • Robotics & Smart Systems
  • Science
  • Sports
  • Startup
  • Tech
  • Travel
  • World

Recent News

Can One AI Platform Replace Your Creative Tool Stack?

Can One AI Platform Replace Your Creative Tool Stack?

2026-01-10
Federated Learning, Part 1: The Basics of Training Models Where the Data Lives

Federated Learning, Part 1: The Basics of Training Models Where the Data Lives

2026-01-10
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2026 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result

© 2026 JNews - Premium WordPress news & magazine theme by Jegtheme.