Close Menu
    Trending
    • Creating AI that matters | MIT News
    • Scaling Recommender Transformers to a Billion Parameters
    • Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know
    • Is RAG Dead? The Rise of Context Engineering and Semantic Layers for Agentic AI
    • ChatGPT Gets More Personal. Is Society Ready for It?
    • Why the Future Is Human + Machine
    • Why AI Is Widening the Gap Between Top Talent and Everyone Else
    • Implementing the Fourier Transform Numerically in Python: A Step-by-Step Guide
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » Implementing the Coffee Machine in Python
    Artificial Intelligence

    Implementing the Coffee Machine in Python

    ProfitlyAIBy ProfitlyAISeptember 8, 2025No Comments10 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    of the best programming languages, and one which encapsulates inside itself variety and the potential to incorporate code as complicated as they arrive. Whereas there are a variety of initiatives and tutorials on beginner-friendly Python-based coding, on this article, we are going to study to construct a Espresso Machine program in Python. This text will present an understanding and apply of conditional statements, loops, and Python dictionaries and can function a foundation for complicated coding (in a later article).

    Understanding the Venture Necessities

    First issues first, allow us to attempt to perceive how this system will work, what the essential necessities are, what variables and conditionals we are going to want, and the scope of the Espresso Making Machine.

    This system will perform as follows: This system will show menu gadgets and ask the person if they need a drink. The person might select a drink of their liking.

    If the person chooses a drink, we must be certain our espresso machine has sufficient sources to make the espresso. If it has, then we are going to proceed forward. In any other case, we are going to let the person know.

    If the sources are sufficient, then we are going to ask the person for cost within the type of cash of nickels, pennies, dimes, and quarters. We are going to calculate the cost made versus the associated fee. If the cost is full, we are going to make the espresso and serve it. If the cost made is greater than the worth of the espresso, we are going to give them their change again. In any other case, if the cost falls wanting the worth, we are going to reject the order and provides again their cash.

    An exception we are going to add is that if somebody from administration desires to know the sources left, or the cash the machine has saved after finishing orders, they could entry this by typing ‘report’. Furthermore, if the administration desires to modify off the machine utterly, they could achieve this by typing ‘off’

    Allow us to outline all this with the assistance of a flowchart:

    Flowchart (Picture by Writer)

    Step 1: Defining Menu & Assets

    Step one in coding this Espresso Machine Venture is to outline the menu in addition to the sources the machine has to organize any order. In our instance, we can have 3 gadgets on the menu: Latte, Espresso, and Cappuccino.

    We are going to use the Python dictionary to be able to outline the menu variable. A Python dictionary is a helpful knowledge kind that shops knowledge towards a key, each of that are simply accessible. The menu variable will likely be a dictionary that not solely shops the three menu gadgets, ie, latte, cappuccino, and espresso, but additionally describes the elements which might be required to make them and their value. Allow us to code the above:

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

    The subsequent job is to outline the sources now we have. These are the sources that may be required to make the various kinds of espresso, in addition to the cash that’s generated by promoting the espresso.

    sources = {
        "water": 1000,
        "milk": 1000,
        "espresso": 100,
        "cash": 0
    }

    As you may see, now we have particular quantities of every ingredient as our sources, and 0 cash as no espresso has been bought initially.

    Step 2: Ask Person for Order

    The subsequent step is to ask the person what they wish to order. We are going to use the Python input function for this goal. Furthermore, we are going to convert the enter string to lowercase so it will likely be simply matched in our conditionals that can observe.

    order = enter("What would you prefer to order?n Cappuccino, Latte or Espresso?n").decrease()
    

    Step 3: Add Particular Instances utilizing Conditionals

    Subsequent, we are going to add 2 particular instances. The primary one is that if the administration desires to show the machine utterly off, they are going to enter off as enter to the above assertion, and the machine will flip off, or in different phrases, this system will finish. We are going to outline a variable for this goal known as finish that will likely be False initially and can flip True when the administration desires to show off the machine.

    One other case we are going to add right here is when the administration wish to know the sources within the espresso machine, they are going to enter report, and the machine will print a report of the obtainable sources. Allow us to put these instances to code:

    if order == 'off':
        finish = True
    elif order == 'report':
        print(f"We now have the next sources:nWater: {sources['water']}mlnMilk: {sources['milk']}mlnCoffee: {sources['coffee']}g nMoney: ${sources['money']}")

    Word that now we have used n and f-string to correctly format the report. Consequence can be:

    Report (Picture by Writer)

    Step 4: Verify Assets

    The subsequent step is to verify the sources required to make espresso. If an espresso requires 50ml of water and 20g of espresso, and both of the elements is inadequate, we can’t proceed with making espresso. Solely when the elements are there’ll we proceed in direction of making espresso.

    That is going to be prolonged to code, we are going to verify one after the other whether or not every of the elements are there in our sources:

    if sources['water'] >= menu[order]['ingredients']['water']:
        if sources['milk'] >= menu[order]['ingredients']['milk']:
            if sources['coffee'] >= menu[order]['ingredients']['coffee']:
                #We are going to put together order
            else:
                print("nResources inadequate! There may be not sufficient espresso!n")
        else:
            print("nResources inadequate! There may be not sufficient milk!n")
    else:
        print("nResources inadequate! There may be not sufficient water!n")

    Solely when the three situations are checked, then we are going to proceed with making the espresso; in any other case, print to the person which of the elements isn’t enough. Additionally, now we have to guarantee that the person has paid the worth of their order. Allow us to try this.

    Step 5: Ask and Calculate Fee

    For the situation listed above, the place all sources can be found, the subsequent step is to ask the person to pay the worth. As soon as the cost is finished, we are going to make the espresso.

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

    Now we are going to ask the person to insert cash (our espresso machine is outdated, so bear with the clinking of the cash :P). We are going to ask for the cash they’re inserting and calculate the overall, then evaluate it with the worth of their order.

    (Picture by Erik Mclean on Unsplash)

    Cash inserted within the espresso machine are of 4 differing types:

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

    The complete worth will likely be calculated by multiplying the variety of coin varieties by their values. Make certain to transform the enter into an int kind; in any other case, you’ll have an error.

    print("Insert cash")
                    p = int(enter("Insert pennies"))
                    n = int(enter("Insert nickels"))
                    d = int(enter("Insert dimes"))
                    q = int(enter("Insert quarters"))
                    complete = (p * 0.01) + (n * 0.05) + (d * 0.10) + (q * 0.25)

    Subsequent, is to verify whether or not the complete quantity calculated is the same as the worth of the espresso chosen or not? Right here is how we are going to code it:

    if complete == menu[order]['price']:
        print("Transaction profitable. Right here is your espresso!")
    elif complete > menu[order]['price']:
        change = complete - menu[order]['price']
    print(f"You've gotten inserted further cash. Right here is your change ${change}n")
        else:
    print("Fee not full. Can't course of order")

    Solely when the quantity is the same as the worth of the espresso, is the transaction profitable. If the complete worth is larger than the worth, we might want to return the change to the person. In any other case, if the overall worth falls wanting the worth, we are going to cancel the order.

    Step 6: Make Espresso

    The final step is to dispense the espresso to the person, and in our coding world, we are going to achieve this by updating each the elements and the cash in our sources dictionary.

    if complete == menu[order]['price']:
        sources['water'] = sources['water'] - menu[order]['ingredients']['water']
        sources['coffee'] = sources['coffee'] - menu[order]['ingredients']['coffee']
        sources['milk'] = sources['milk'] - menu[order]['ingredients']['milk']
        sources['money'] = sources['money'] + menu[order]['price']
        print("Transaction profitable. Right here is your espresso!")

    Discover that the elements are subtracted from the sources whereas cash is added. Therefore, our sources dictionary is up to date with every order delivered. The identical situation can even be added when now we have an extra quantity and are required to present change again.

    Step 7: Program Continuity

    After the espresso order is processed, so long as the administration doesn’t command the espresso machine to modify off, the machine will carry on asking the person for his or her espresso order, processing funds, updating sources, and allotting the espresso. So we are going to embrace some time loop to make sure this system continues after the drink has been distributed.

    The whole block of code now we have coded above will likely be included on this whereas loop:

    whereas finish != True:
        order = enter("nWhat would you prefer to order?nCappuccino, Latte or Espresso?n").decrease()
    
        if order == 'off':
            finish = True
        elif order == 'report':
            print(f"We now have the next sources:nWater: {sources['water']}mlnMilk: {sources['milk']}mlnCoffee: {sources['coffee']}g nMoney: ${sources['money']}")
    
        elif sources['water'] >= menu[order]['ingredients']['water']:
            if sources['milk'] >= menu[order]['ingredients']['milk']:
                if sources['coffee'] >= menu[order]['ingredients']['coffee']:
                        print(f"Pay ${menu[order]['price']}n")
    
                        # TODO : Cash insert
                        print("Insert cash")
                        p = int(enter("Insert pennies"))
                        n = int(enter("Insert nickels"))
                        d = int(enter("Insert dimes"))
                        q = int(enter("Insert quarters"))
                        complete = (p * 0.01) + (n * 0.05) + (d * 0.10) + (q * 0.25)
    
                        if complete == menu[order]['price']:
                            sources['water'] = sources['water'] - menu[order]['ingredients']['water']
                            sources['coffee'] = sources['coffee'] - menu[order]['ingredients']['coffee']
                            sources['milk'] = sources['milk'] - menu[order]['ingredients']['milk']
                            sources['money'] = sources['money'] + menu[order]['price']
                            print("Transaction profitable. Right here is your espresso!")
                        elif complete > menu[order]['price']:
                            change = complete - menu[order]['price']
                            print(f"You've gotten inserted further cash. Right here is your change ${change}n")
                        else:
                            print("Fee not full. Can't course of order")
    
                else:
                    print("nResources inadequate! There may be not sufficient espresso!n")
            else:
                print("nResources inadequate! There may be not sufficient milk!n")
        else:
            print("nResources inadequate! There may be not sufficient water!n")

    Conclusion

    We now have efficiently transformed the working of a espresso machine into Python code. Over the course of this venture, we explored dictionaries and accessing them, conditional statements, and the whereas loop. Whereas fairly easy, we are able to additional simplify this venture with different methods reminiscent of Object Oriented Programming (a venture for subsequent time). Do share your suggestions relating to this venture and any options on how we are able to additional enhance it. Until then, take pleasure in your espresso!

    (Picture by Nathan Dumlao on Unsplash)

    Entry the complete code right here: https://github.com/MahnoorJaved98/Coffee-Machine/blob/main/Coffee%20Machine%20-%20Python.py



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow Yichao “Peak” Ji became a global AI app hitmaker
    Next Article The Definitive Guide to Data Parsing
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Creating AI that matters | MIT News

    October 21, 2025
    Artificial Intelligence

    Scaling Recommender Transformers to a Billion Parameters

    October 21, 2025
    Artificial Intelligence

    Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

    October 21, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    AI verktyg för fitness diet och träningsupplägg

    April 24, 2025

    Are You Being Unfair to LLMs?

    July 11, 2025

    AI learns how vision and sound are connected, without human intervention | MIT News

    May 22, 2025

    Help! My therapist is secretly using ChatGPT

    September 9, 2025

    Beyond KYC: AI-Powered Insurance Onboarding Acceleration

    August 21, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    Taking ResNet to the Next Level

    July 3, 2025

    How to Enrich LLM Context to Significantly Enhance Capabilities

    September 16, 2025

    How to Use AI to Break Free From Data Paralysis with Katie Robbert [MAICON 2025 Speaker Series]

    August 7, 2025
    Our Picks

    Creating AI that matters | MIT News

    October 21, 2025

    Scaling Recommender Transformers to a Billion Parameters

    October 21, 2025

    Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

    October 21, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.