# Stuck on 11/13

#1

This one seems really simple, I just can’t remember all these little commands and ways of formatting code.
I am racking my brain and flipping through my notes–I even watched a YouTube video about for-loops, I just cannot figure it out: How do I make the for-loop apply everything to a value? All I can get it to do is try to add the key to the total, which isn’t working for obvious reasons.
What I have so far is:

def compute_bill(food):
[indent]total = 0
[indent]for x in prices:
[2x-indent]x += total

It seems like this is trying to do arithmetic with strings, so naturally it’s returning an error. My problem is I cannot figure out what combination of a variable and brackets will solve this problem.
I know it’ll look like

Key_or_value_or_something[key_or_value_in_brackets]

but I can’t figure out what goes in the brackets and what goes before them.

#2

to get a value from a dictionary we have the following general syntax:

``````dict[key]
``````

where key can be a string or a variable containing a string.

#3

Doesn’t dict[key] only apply to individual keys? Is it possible to make the for-loop iterate through every key in the dictionary using dict[key]? Or will I have to make an itemized list of every individual key I want the for-loop to iterate over?
Edit: To be more specific,

prices[x] += total

seems to be throwing an error. I’m pretty sure I shouldn’t be referring to the dictionary that I’m applying the for-loop to, but I can’t figure out what else it could possibly be, if not prices[x] += total or total = total + prices[x].

#4

Is it possible to make the for-loop iterate through every key in the dictionary? Yes it is.

given the loop iterable (`x`) will contain all the different keys (one at a time), we can use the loop iterator to get all the values and add them to total

which is what you do with `total = total + prices[x]`, that should work

#5

Thank you. That’s helpful.
I still haven’t solved the exercise, there must be some question I don’t even know yet that I need to ask.
It’s frustrating that there are no hints on this one. I feel like I’m overlooking something major and my hand will fly on its own to slap my forehead when I see it.

#6

oops, i am not paying attention.

why are you looping over `prices`?

#7

For each item in the food list I’m supposed to add that item’s price to total. Since prices is the dictionary that has either food or prices in it, I figured that’s what I was supposed to be using. I’m pretty sure I’m supposed to make a sum of the values in prices.
I feel like I should probably do something with that word “food” that appears as the argument on the function I define, but I can’t see where it should go. Maybe some kind of “food =” type statement before the for-loop?
Edit: Ohhhh my god… I’ve been working on this for so long, I forgot all about the shopping_list at the top of the editor. I’m still lost, but this is something crucial I have completely overlooked for two hours.

#8

This is where earlier knowledge comes in.

`food` is the function parameter which gets its value from argument from function call. Functions only execute when called, thus `food` is only a placeholder until it gets value from argument at function call

go add a function call and give it an argument of `shopping_list` (which you defined on line 1)

then in the first line of the function, `print food` to see what happens

#9

Does “add a function call” mean I should add a line that just says the name of the function, in order for it to be executed? In this case it’d look like:

compute_bill(food)

then see what happens? Because I think my compute_bill() function isn’t operational, so calling it (did I use that term right?) just gives me an error.
Edit: I changed my function so all it says is:

def compute_bill(food):
[indent] print food

and all it does is print [‘apples’] which… blows my mind, because it seems like food is not connected at all, to anything. Python shouldn’t “know” that I intended food to be in any way connected with the dictionaries above.
Seriously, this does not make any sense. How does it know “apple” is connected to “food”? if there is no code whatsoever linking them? Screen capture:

#10

Functions only execute when called. The value you pass as argument at function call needs to exist, given the argument is passed to the parameter (`food`)

You could have kept the rest of your code, i just asked you to add a function call and a print statement.

how do you think the exercise validates your code is correctly? Calling the function and checking the returned returned result is right, the apple you see is a function call from the exercise validation.

its not connected to the dictionary, `food` will be a list of products a customer wants to buy. In fact, we can have multiple function calls, each representing a different customer who wants to buy different products.

`shopping_list` can be a good example in this matter, you could use this argument for your function call.

#11

So it isn’t receiving the word `apple` from anything in the code? If I’m understanding you correctly, you’re saying `"apple"` came up (even though it was not associated with my code) as a result of the Codecademy program checking to see if I solved the exercise?

#12

Exactly, the exercise did this:

``````compute_bill(['apple'])
``````

to validate your code. But that shouldn’t be the most the most important aspect of my answer, focus on your code and add your own function call

#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.