Skip to main content

365 DAYS OF CODE - DAY 22

Modified code from yesterday.

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(water_needed, milk_needed, coffee_needed):
if choice == "espresso":
if water_needed < resources["water"]:
print("Sorry, there is not enough water.")
elif coffee_needed < resources["coffee"]:
print("Sorry, there is not enough coffee.")


elif choice == "latte":
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.")

elif choice == "cappuccino":
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.")


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

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"]

elif choice == "off":
break

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

else:
print("Sorry, that's not a valid input. Please try again.")
break

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

resource_sufficient(water_needed, milk_needed, coffee_needed)

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

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

resources["water"] = resources["water"] - water_needed
resources["milk"] = resources["milk"] - milk_needed
resources["coffee"] = resources["coffee"] - coffee_needed
resources["money"] = resources["money"] + coffee_price

I decided to take away as much functions as I could and did it my way. It worked but I have to strengthen my understanding of functions. :)

I also do not like that I have to call every number holding variable at the top of my code.

At first logging in made sure I did not skip a day but now it is feeling more like a chore. It gets better though.

I can do it!! :)



Comments

Popular posts from this blog

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...

365 DAYS OF CODE - DAY 13

What is wrong with this code?? Can you tell what it does?? import art n1 = 0 n2 = 0 selected_operation = "" solution = 0 def add (n1, n2): return n1 + n2 def subtract (n1, n2): return n1 - n2 def multiply (n1, n2): return n1 * n2 def divide (n1, n2): return n1 / n2 operations = { "+" : add, "-" : subtract, "*" : multiply, "/" : divide } def rest_of_calculation (): print ( "+ \n - \n * \n /" ) selected_operation = input ( "Pick an operation: " ) n2 = float ( input ( "What is the next number?: " )) for key in operations: if key == selected_operation: solution = operations[key](n1, n2) print ( f" { n1 } { key } { n2 } = { solution } " ) print (art.logo) n1 = float ( input ( "What is the first number?: " )) rest_of_calculation() while True : new_calculation = input ( f"Type 'y' to continue calcu...

365 DAYS OF CODE - DAY 10

I realized that I have been tagging each day in accordance with the 100 days of python course . Now, I am one day behind (it is day 10 of the 365 days of code but day 9 on the python course I am taking right now) because I made sure to review unclear exercises yesterday. It is fine regardless. On day 9 of the python course, we looked at dictionaries, its table-likeness (2 columned) One tiny forgotten comma leads to 4 different errors and 1 warning, the prominent one being the SyntaxError. But shouldn’t it highlight line 3 instead of line 4?? Comma added, Error fixed. My Grading Program Code: student_scores = {     'Harry': 88,     'Ron': 78,     'Hermione': 95,     'Draco': 75,     'Neville': 60 }   student_grades = {}   for key in student_scores:     if student_scores[key] > 90:         student_scores[key] = "O...