Guest User

Untitled

a guest
Jun 14th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.52 KB | None | 0 0
  1. MENU = {
  2.     "espresso": {
  3.         "ingredients": {
  4.             "water": 50,
  5.             "coffee": 18,
  6.         },
  7.         "cost": 1.5,
  8.     },
  9.     "latte": {
  10.         "ingredients": {
  11.             "water": 200,
  12.             "milk": 150,
  13.             "coffee": 24,
  14.         },
  15.         "cost": 2.5,
  16.     },
  17.     "cappuccino": {
  18.         "ingredients": {
  19.             "water": 250,
  20.             "milk": 100,
  21.             "coffee": 24,
  22.         },
  23.         "cost": 3.0,
  24.     }
  25. }
  26.  
  27. # czemu nagle jest małymi literami?
  28. resources = {
  29.     "water": 300,
  30.     "milk": 200,
  31.     "coffee": 100,
  32.     "money": 0,
  33. }
  34.  
  35. COIN_VALUES = {
  36.     "quarters": 0.25,
  37.     "dimes": 0.1,
  38.     "nickles": 0.05,
  39.     "pennies": 0.01,
  40. }
  41.  
  42.  
  43. # Po co ten type annotation?
  44. def inserted_coin_value() -> object:
  45.     """Asks the user for the coins and returns their total value"""
  46.     print("Please insert coins. ")
  47.     total = 0
  48.     for coin in COIN_VALUES:
  49.         how_many_coins = int(input(f"How many {coin}?: "))
  50.         total += how_many_coins * COIN_VALUES[coin]
  51.     return total
  52.  
  53.  
  54. def is_transaction_successful(total, user_coffee_choice):
  55.     """Checks whether the user can afford the product. True when can, False when can't. """
  56.     # używaj .get() zamiast bezpośrednio indeksować dicty
  57.     if MENU[user_coffee_choice]["cost"] <= total:
  58.         resources["money"] = resources["money"] + drink["cost"]
  59.         return True
  60.     else: # zbędny else
  61.         return False
  62.  
  63.  
  64. def are_resources_sufficient(drink):
  65.     """ Checks whether there is enough ingredients for each of the products and returns True when the order can be made, False when not."""
  66.     for ingredient in drink["ingredients"]:
  67.         if resources[ingredient] < (drink["ingredients"][ingredient]):
  68.             # Lepiej, żeby funkcje nie miały side effectów w postaci
  69.             # wypisywania tekstu, jeżeli tylko jest taka możliwość
  70.             # Teksty wypisuj w głównej pętli i opieraj się na tym, co zwraca
  71.             # funkcja
  72.             print(f"Sorry, there's not enough {ingredient}.")
  73.             return False
  74.         return True
  75.  
  76.  
  77. def print_report():
  78.     """Prints current report on resources"""
  79.     print(f"Water: {resources['water']}ml")
  80.     print(f"Milk: {resources['milk']}ml")
  81.     print(f"Coffee: {resources['coffee']}g")
  82.     print(f"Money: ${resources['money']}")
  83.  
  84.  
  85. def make_coffee():
  86.     """Removes amt of ingredients from resources{}. """
  87.     for ingredient in drink["ingredients"]:
  88.         resources[ingredient] = resources[ingredient] - drink["ingredients"][ingredient]
  89.  
  90.  
  91.  
  92. is_on = True
  93.  
  94. while is_on:
  95.     # Zahardkodowane ceny i nazwy produktów, które mógłbyś wziąć z MENU
  96.     user_coffee_choice = str(input("What would you like? espresso ($1.5)/latte ($2.5)/cappuccino ($3.0): "))
  97.     if user_coffee_choice == "report":
  98.         print_report()
  99.     elif user_coffee_choice == "off":
  100.         is_on = False
  101.     else:
  102.         # znowu, lepiej MENU.get(), bo może wystąpić KeyError
  103.         drink = MENU[user_coffee_choice]
  104.         if are_resources_sufficient(drink):
  105.             total = inserted_coin_value()
  106.             if is_transaction_successful(total, user_coffee_choice):
  107.                 make_coffee()
  108.                 if total > drink["cost"]:
  109.                     change = round(total - drink["cost"], 2)
  110.                     print(f"Here's ${change} in change")
  111.                 print(f"Here's your {user_coffee_choice}. Enjoy!")
  112.             else:
  113.                 print("Sorry, that's not enough money. Money refunded.")
Advertisement
Add Comment
Please, Sign In to add comment