#🔒 back with the coffee machine, but a new problem ;-;

72 messages · Page 1 of 1 (latest)

slate pagoda
#

i want to do mathematical operations between two sub dictionaries in respective dictionaries

granite edgeBOT
#

@slate pagoda

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slate pagoda
#

!pastebin

granite edgeBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

earnest trout
#

history help - shows menu
history clear - clears ai chat history
history get <id> - gets an history item

#

history help - shows menu
history clear - clears ai chat history
history get <id> - gets an history item

slate pagoda
#

damn ok

rustic lotus
fierce sun
#

I noticed this last time you posted your code. You can't reassign a global dictionary from a function

slate pagoda
fierce sun
#

well, you start with 0 of all ingredients

#

shouldn't you start with something?

slate pagoda
#

that is the empty value i want to assign as the ingredients in "coffee" value put by user

#

come to think of it, i should do that first

fierce sun
#
left_ingredients = display_ingredients(MENU[coffee["ingredients"]])
#

well here, you're assigning left_ingredients to the result of display_ingredients()

#

why?

#
def display_ingredients(stuff):
    for key, value in stuff.items():
        print(f"{key} : {value}")
#

that function doesn't return anything

#

but also I'm not sure why you would want to do that anyways

slate pagoda
#

i was testing something out haha

fierce sun
#

we can't decipher what was just for testing and what is meant to be your actual logic

slate pagoda
#

i just wanted to check if it would display correctly

#

well, that out of the way

#
    print(f"you need this and that for {coffee}")
    left_ingredients = (MENU[coffee]["ingredients"])
    print(left_ingredients)```
#

this is the new code

#

it just assigns the left_ingredients to the coffee ingredients

#

what i want is that the resources to get subtracted by the coffee ingredients once i order it

fierce sun
#

it seems this function's sole responsibility should be displaying the ingredient cost. It shouldn't do anything to your actual resources

#

you can create another function for that

slate pagoda
#

no no

fierce sun
#

I don't know your exact requirements though

slate pagoda
#

alright

#

let me tell you whats going on

#

-first of, what i wanted the function to do is display the ingredients left in our left_resources dict after the coffee ingredients dict have been assigned to it subtracted by our resources dict

exotic bane
#

@fierce sun if you don’t mind could you take a look at my question after you’re done with this

fierce sun
exotic bane
fierce sun
#

why do you need it?

slate pagoda
fierce sun
#

you shouldn't need that there

#

that would happen in your main loop

slate pagoda
#

oh

#

so

#

how do i subtract my resources with the coffee ingredients?

fierce sun
#

using a loop

#

loop through the ingredients required for that drink choice

#

then access that same key in your resources and subtract that amount

#

you should first verify that you have all the necessary ingredients

slate pagoda
#

i hate loops 😭

fierce sun
#

practice them more and you won't hate them so much

slate pagoda
fierce sun
#

yes, if the requirements are more than your resources, then you can return some sort of invalid message

weary holly
# slate pagoda i hate loops 😭

Loops are foundational. You'll need to use them pretty regularly for anything non-trivial. You should just work to practice them so you have a better understanding of them.

fierce sun
#

so you'll want to split up your logic here

slate pagoda
#

okay

fierce sun
#

first check if you have the required ingredients

fierce sun
#

return True if you do or False if you don't

#

if True was returned, then you can subtract your ingredients

slate pagoda
#

alright

fierce sun
#

maybe see if you can work out the logic in a separate file, then try and integrate it into your main program

#

it makes testing quicker

slate pagoda
#

thanks guys

#

!close

granite edgeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.