DEV Community

Palak Hirave
Palak Hirave

Posted on

Day 15 of 100

Today I just practiced once more everything that I learnt. This time in a form of a coffee machine minus the actual thing. It took just a bit over an hour and wasn't too hard but as an area of improvement for next time, I should be able to reduce the lines of code that I used and make the overall program smaller whilst still doing the same things. I didn't learn into any paticular challenges in this one but I did figure out about the betrayal of the pass statement. All this while I thought a pass statement would skip an iteration of a while loop but when my program was not having the desired effect, a quick search told me that I was indeed wrong. A pass statement is basically a useless placeholder. So continue took its place and now I have a logically sound code. Still, such a betrayal from pass, I prized you for being usefull but I guess not.

I want a latte after all this.

Anyways, here's the code:

MENU = {
    "espresso": {
        "ingredients": {
            "water": 50,
            "coffee": 18,
        },
        "cost": 1.5,
    },
    "latte": {
        "ingredients": {
            "water": 200,
            "milk": 150,
            "coffee": 24,
        },
        "cost": 2.5,
    },
    "cappuccino": {
        "ingredients": {
            "water": 250,
            "milk": 100,
            "coffee": 24,
        },
        "cost": 3.0,
    }
}

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

# TODO Report
def give_report():
    print(f" Water: {resources['water']}ml \n Milk: {resources['milk']}ml \n Coffee: {resources['coffee']}g \n Money: ${resources['money']}")

end = 0
# TODO Sufficient Resources?
def sufficient_resources():
    global end
    if order == "espresso":
        if 50 > resources['water']:
            print("Sorry there is not enough water")
            end = 1
        else:
            resources['water'] -= 50

        if 18 > resources['coffee']:
            print("Sorry there is not enough coffee")
            end = 1
        else:
            resources['coffee'] -= 18

    elif order == "cappuccino":
        if 250 > resources['water']:
            print("Sorry there is not enough water")
            end = 1
        else:
            resources['water'] -= 250

        if 24 > resources['coffee']:
            print("Sorry there is not enough coffee")
            end = 1
        else:
            resources['coffee'] -= 24

        if 100 > resources['milk']:
            print("Sorry there is not enough milk")
            end = 1
        else:
            resources['milk'] -= 100

    elif order == "latte":
        if 200 > resources['water']:
            print("Sorry there is not enough water")
            end = 1
        else:
            resources['water'] -= 200

        if 24 > resources['coffee']:
            print("Sorry there is not enough coffee")
            end = 1
        else:
            resources['coffee'] -= 24

        if 150 > resources['milk']:
            print("Sorry there is not enough milk")
            end = 1
        else:
            resources['milk'] -= 150

# TODO Get Coins
quarter = 0
dime = 0
nickel = 0
pennies = 0
total_user_money = 0

def get_coins():
    global quarter, dime, nickel, pennies, total_user_money

    quarter = int(input("How many quarters?: "))
    dime = int(input("How many dimes?: "))
    nickel = int(input("How many nickels?: "))
    pennies = int(input("How many pennies?: "))

    total_user_money = quarter*0.25 + dime*0.1 + nickel*0.05 + pennies*0.01

# TODO Transfer
def enough_money(order):
    global total_user_money

    if order == "espresso":
        if total_user_money <= 1.5:
            print("Sorry that's not enough money. Money refunded.")
        else:
            resources['money'] += 1.50
            total_user_money -= 1.50
            total_user_money = round(total_user_money, 2)
            print(f"Here is the change: ${total_user_money}")
            print("Here is your espresso. Enjoy!")

    if order == "cappuccino":
        if total_user_money <= 3.0:
            print("Sorry that's not enough money. Money refunded.")
        else:
            resources['money'] += 3.00
            total_user_money -= 3.00
            total_user_money = round(total_user_money, 2)
            print(f"Here is the change: ${total_user_money}")
            print("Here is your cappuccino. Enjoy!")

    if order == "latte":
        if total_user_money <= 2.5:
            print("Sorry that's not enough money. Money refunded.")
        else:
            resources['money'] += 2.50
            total_user_money -= 2.50
            total_user_money = round(total_user_money, 2)
            print(f"Here is the change: ${total_user_money}")
            print("Here is your latte. Enjoy!")


# TODO Make coffee
on = True
while on != False:
    order = input("What would you like? (espresso, latte, cappuccino): ")
    if order == "report":
        give_report()

    elif order == "espresso":
        sufficient_resources()
        if end == 1:
            continue
        print("Please insert coins.")
        get_coins()
        enough_money(order)

    elif order == "latte":
        sufficient_resources()
        if end == 1:
            continue
        print("Please insert coins.")
        get_coins()
        enough_money(order)

    elif order == "cappuccino":
        sufficient_resources()
        if end == 1:
            continue
        print("Please insert coins.")
        get_coins()
        enough_money(order)

    elif order == "off":
        on = False

    else:
        print("Invalid input. Please try again.")

Enter fullscreen mode Exit fullscreen mode

Which programs/projects are you guys up to?

Top comments (0)