OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Substract values dictionary from another dictionary's values

  • Thread starter Thread starter gringocoder
  • Start date Start date
G

gringocoder

Guest
I have a coffee machine that gives a user 3 options to choose from: espresso, latte, cappuccino. The ingredients and cost of the chosen item are stored in a dictionary in the variable MENU. After the user has chosen his coffee I need to be able to print a report which tells me how much resources are left in the machine, the resources being water, milk, coffee. How can I subtract the ingredients (ingredients chosen coffee) from the resources variable and get the remaining resources printed in a report?

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

From previous lessons, I learned that I need to format the data from the dictionary so that it becomes usable in the code. But I'm at a very beginner level and I'm stuck after formatting the data.

Code:
def format_data(choice):
    """Format ingredients into printable format: water, milk, coffee, cost"""
    water = MENU[user_choice]['ingredients']["water"]
    milk = MENU[user_choice]['ingredients']["milk"]
    coffee = MENU[user_choice]['ingredients']["coffee"]
    cost = MENU[user_choice]['ingredients']["cost"]

user_choice = input("What would you like? (espresso/latte/cappuccino").lower()
<p>I have a coffee machine that gives a user 3 options to choose from: espresso, latte, cappuccino. The ingredients and cost of the chosen item are stored in a dictionary in the variable MENU. After the user has chosen his coffee I need to be able to print a report which tells me how much resources are left in the machine, the resources being water, milk, coffee. How can I subtract the ingredients (ingredients chosen coffee) from the resources variable and get the remaining resources printed in a report?</p>
<pre class="lang-py prettyprint-override"><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,
}
</code></pre>
<p>From previous lessons, I learned that I need to format the data from the dictionary so that it becomes usable in the code. But I'm at a very beginner level and I'm stuck after formatting the data.</p>
<pre class="lang-py prettyprint-override"><code>def format_data(choice):
"""Format ingredients into printable format: water, milk, coffee, cost"""
water = MENU[user_choice]['ingredients']["water"]
milk = MENU[user_choice]['ingredients']["milk"]
coffee = MENU[user_choice]['ingredients']["coffee"]
cost = MENU[user_choice]['ingredients']["cost"]

user_choice = input("What would you like? (espresso/latte/cappuccino").lower()
</code></pre>
 

Latest posts

Top