Skip to main content

365 DAYS OF CODE - DAY 21

I tried using functions for the coffee machine program, but it's just not working out well. Especially the fact that you cannot break out of it.

My present code for today.

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,
}


coffee_price = 0
quarters = 0
dimes = 0
nickels = 0
pennies = 0
water_needed = 0
milk_needed = 0
coffee_needed = 0


def report():
"""outputs a present state report of the coffee machine"""
print(f"Water: {resources["water"]}")
print(f"Milk: {resources["milk"]}")
print(f"Coffee: {resources["coffee"]}")
print(f"Money: {resources["money"]}")


def resource_sufficient():
"""checks the amount of resources remaining with the amount of resources needed to make the selected coffee"""
if water_needed > resources["water"]:
print("Sorry, there is not enough water.")
elif milk_needed > resources["milk"]:
print("Sorry, there is not enough milk.")
elif coffee_needed > resources["coffee"]:
print("Sorry, there is not enough coffee.")


def coin_collector_and_calculator():
"""collects coins, calculates total amount inserted and returns change or refunds where necessary"""
print("Please insert coins.")
quarters = float(input("how many quarters?: "))
dimes = float(input("how many dimes?: "))
nickels = float(input("how many nickels?: "))
pennies = float(input("how many pennies?: "))

total_quarters = 0.25 * quarters
total_dimes = 0.10 * dimes
total_nickels = 0.05 * nickels
total_pennies = 0.01 * pennies

total_amount_inserted = total_quarters + total_dimes + total_nickels + total_pennies

if total_amount_inserted < coffee_price:
print("Sorry that's not enough money. Money refunded.")

else:
make_coffee()
change = total_amount_inserted - coffee_price
print(f"Here is ${change:.2f} dollars in change.")
print(f"Here is your {choice}. Enjoy!")


def make_coffee():
"""creates coffee and calculates the amount of ingredients used and total money collected"""
global water_needed, milk_needed, coffee_needed, coffee_price
if choice == "espresso":
water_needed = MENU["espresso"]["ingredients"]["water"]
milk_needed = 0
coffee_needed = MENU["espresso"]["ingredients"]["coffee"]
coffee_price = MENU["espresso"]["cost"]

elif choice == "latte":
water_needed = MENU["latte"]["ingredients"]["water"]
milk_needed = MENU["latte"]["ingredients"]["milk"]
coffee_needed = MENU["latte"]["ingredients"]["coffee"]
coffee_price = MENU["latte"]["cost"]

elif choice == "cappuccino":
water_needed = MENU["cappuccino"]["ingredients"]["water"]
milk_needed = MENU["cappuccino"]["ingredients"]["milk"]
coffee_needed = MENU["cappuccino"]["ingredients"]["coffee"]
coffee_price = MENU["cappuccino"]["cost"]

update_report()


def update_report():
"""updates the coffee machine's report content by taking away used items and adding the money paid for the coffee"""
resources["water"] = resources["water"] - water_needed
resources["milk"] = resources["milk"] - milk_needed
resources["coffee"] = resources["coffee"] - coffee_needed
resources["money"] = resources["money"] + coffee_price


while True:
choice = input("What would you like? (espresso/latte/cappuccino): ")

if choice == "off":
break

if choice == "report":
report()
break

resource_sufficient()

coin_collector_and_calculator()


To be modified tomorrow. :)

Comments

Popular posts from this blog

Some Exercises on Python Turtle Module

Day 44 Drawing dotted lines using the turtle module: from turtle import Turtle, Screen maddy = Turtle() maddy.shape( "turtle" ) maddy.color( "green" ) maddy.pencolor( "black" ) for i in range ( 50 ): maddy.forward( 3.5 ) maddy.penup() maddy.forward( 3.5 ) maddy.pendown() screen = Screen() screen.exitonclick() Drawing shapes: from turtle import Turtle, Screen import random colors = [ "red" , "green" , "blue" , "yellow" , "cyan" , "magenta" , "orange" , "violet" , "brown" , "black" ] maddy = Turtle() maddy.shape( "turtle" ) maddy.color( "green" ) draw = True sides = 2 while draw: maddy.pencolor(random.choice(colors)) if sides <= 10 : sides += 1 for i in range (sides): maddy.forward( 70 ) maddy.right( 360 / sides) else : draw = False screen = Screen() screen.exito...

Extracting Colors from Images in Python using the colorgram.py module

Day 45 My Code: import colorgram colors = colorgram.extract( "image.jpg" , 1000 ) color_list = [] for _ in range ( len (colors)): color_list.append(colors[_].rgb) print (color_list) Output: [Rgb(r=250, g=234, b=220), Rgb(r=216, g=103, b=19), Rgb(r=67, g=171, b=214), Rgb(r=231, g=167, b=72), Rgb(r=200, g=122, b=181), Rgb(r=23, g=11, b=6), Rgb(r=251, g=141, b=158), Rgb(r=213, g=98, b=71), Rgb(r=252, g=238, b=243), Rgb(r=245, g=193, b=65), Rgb(r=249, g=252, b=251), Rgb(r=183, g=135, b=53), Rgb(r=5, g=19, b=31), Rgb(r=18, g=6, b=11), Rgb(r=247, g=164, b=142), Rgb(r=235, g=245, b=247), Rgb(r=58, g=106, b=129), Rgb(r=122, g=161, b=143), Rgb(r=5, g=10, b=7), Rgb(r=117, g=80, b=101), Rgb(r=90, g=67, b=27), Rgb(r=191, g=97, b=119), Rgb(r=81, g=145, b=168), Rgb(r=92, g=52, b=43), Rgb(r=162, g=204, b=211), Rgb(r=97, g=42, b=72), Rgb(r=29, g=74, b=92), Rgb(r=90, g=158, b=150), Rgb(r=76, g=100, b=88), Rgb(r=43, g=63, b=92), Rgb(r=170, g=204, b=202), Rgb(r=123, g=117, b=151), Rgb(r=48,...

365 DAYS OF CODE - DAY 0

DAY 0 THOUGHT PROCESS Day Zero of the 365 days of code highlights my state of mind before the actual start of the program. Yes, it is a program. A program I set for myself to make sure I actually put in the work on a daily. I have tried coding before but have never been quite consistent with it. A friend advised me to try coding daily if I really wanted to be great at it someday. He also stated that it would take a long time and I should be prepared for it. During the same period, I noticed he was on a year's worth streak of chess which made me realize I could also do the same but with coding. I signed up for the 100 days of code course on Udemy and plan on starting my coding journey with Angela Yu and the thousands of students also taking the course. I also drafted my own copy of the pledge, copied and modified from the 100 Days of Python Pledge from the course materials. It is currently pasted on my wall as a constant reminder. Finally, I will also be recording my process here...