A common issue here is indenting the return statement inside of the for loop, which causes the loop to only be able to possibly run one time before the function returns and exits.
If your code looks something like the code below, unindent your return statement to be on the same level as the for, not inside of it.
I have been struggling with this the last few exercises so I figured Iâd post. How do you determine how you define âthingsâ in lists/dictionaries when it comes to loop statements? For example, the solution for this problem says " for item in food". How did you come to the conclusion of using the term item? In previous examples, âfor food in pricesâ was used. How was it determined âfoodâ should be used? Thanks.
the for in loop (for variable in list) declares (names) a variable within the for loop, you are free to pick your own variable names, except for the reserved words in a programming language
however, its recommend to give variable logic/descriptive names. This is something that comes with time and experience.
Whoever wrote the course decided on the variable names used, because (s)he thought these names fitted best.
I also have a question on this exercise. My code (below) is coming back rejected.
def compute_bill(food):
total = 0
for p in prices:
total += prices[p]
return total
The tip below the output says compute_bill(['apple']) returned 10.5 instead of 2
Wasnât the point of the exercise to compute all of the food items on the list (Which would give us $10.50). It doesnât make sense that the computer is trying to pass âappleâ as an argument through this function since we already know its price is $2.
shopping_list = ["banana", "orange", "apple"]
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total
print compute_bill(shopping_list)
From my understanding, weâre to assume the customer only wants 1 of each item on their list. 7.5 was printed to my console, which equals the combined cost of 1 apple, 1 orange and 1 banana as defined in the dictionaries.
Did you change the values of the keys in your dictionaries? These lessons donât take kindly to mutations in the provided code, or the use of definitions aside from the ones provided in the lesson outline.
Your function looks essentially identical to mine, so I donât imagine thatâs the problem.
no, there is a one difference between your and kmonroe25 code. Study the for loop closely. He is looping over prices, and not using his food parameter. So if you run his code and add a bunch of function calls you will see the function always returns 8.5, regardless of what customer (function call) wants to buy
The source of the confusion is in the instructions. You are told to add the value of each item in âthe food listâ to your total variable. There are two dictionaries and a shopping list already included in the provided code. None of them are identifiable as a âfood listâ.
The problem with this is that we arenât supposed to use a âlistâ at all, despite the fact that this module is dealing with Python lists and dictionaries. The exercise intends for us to use an argument named âfoodâ as input in our âcompute_billâ function, and to pull only from the âpricesâ dictionary provided. No list is used in this function.
It seems a few people are being misled by this use of the term list in the instructions to total everything in the food list, instead of just being told to use a function with the argument âfoodâ to total everything in the âpricesâ dictionary.
The lessons up to this one are teaching how to create lists, perform basic manipulation of lists, and understand the differences between lists and dictionaries. A person without prior experience in programming who reaches this lesson is unprepared for what they will perceive as a shift in terminology.
The instruction offered up to this point teaches a student to see [1, 2, 3, 4] as a list. When the instructions say âfood listâ, they will look for a variable âfoodâ followed by integers or strings in brackets. This is what they have been taught to do by the prior lessons.
I understand a food list is created in the function. The lessons in this module, however, do not make this clear.
Also, when I pulled the solution to this exercise it did not take the shopping list as an argument when it called the compute_bill function. The solution provided the sum of one item each of the four fruits in the prices dictionary, not the sum of the prices for the three items included in the solutionâs shopping_list.
I agree you provided the correct answer to the presented instructions. Unfortunately, the module did not count that answer as correct. So either the key was incorrect, or the instructions were unclear.
The food list is given to us at the top of the code⌠shopping_list. Clearly we can see it is a list so I donât quite get the confusion. However, I do see your point about how learners expect things to be literal, rather than implied or suggested. More than a few people have stumbled over this question but that is why we have this forum. We can help clarify the question, but are not in a position to argue whether the instructions are clear or not. It wonât change anything. Best we just clear things up, make the most of it and move on.
Self study implies that we will reach for other resources as we learn. It only makes sense that a simple course is going to miss out on a lot of the details. We can discover them by reading the documentation on each new concept and at the same time increase our knowledge base of the available methods on that class of object.
Lists and dictionaries have a lot of methods associated with them. We use only a handful here, but extra study and practice will help grow oneâs toolkit to include other methods not taught here.
It would be nice if in the instructions, it would actually say what weâre trying to make here, because the example with a given list at the end was completely misleading, and having the instructions be listed step-by-step when in reality, half of the instructions is implied, is just bad lesson planning. âThe point of this exercise was to make a function which could calculate the total cost of a customer purchase.â Thatâs all that needed to be said.
Define a function compute_bill that takes one argument food as input.
In the function, create a variable total with an initial value of zero.
For each item in the food list, add the price of that item to total .
Finally, return the total .
Ignore whether or not the item youâre billing for is in stock.Note that your function should work for any food list.
These are the instructions that list everything you need to do, except why youâre doing it. Anyone following the instructions have no idea what to tweak to make it work if weâre not clear what weâre trying to do. On top of that, how are we, up to this point, supposed to know Python is capable of understanding imaginary scenarios? jturneraudit is right because I WAS looking for âfoodâ and wondering where the program was pulling a for loop from. Even after seeing the solution, I did not understand what âfoodâ meant or what it was an argument for, until realizing it is the supposed shopping list of a customer.
The instructions try too hard to be concise that it lacks a lot of vital information, and are so literal anyone following will just try to follow the instructions word-for-word and not understand why their program is missing something, meanwhile not getting the bigger picture. You canât take the time to read the exercise if there isnât any information. Even the example just says the ânâ list runs through the function without explaining why ânâ exists and why anyone is trying to run it through the function.
because this was mentioned in the previous lesson, and this lesson was a continuation of the previous lesson
you are not, but understanding parameters and arguments is quite vital. Which has been taught. Its okay to revisit earlier lessons or documentation if you do not precisely remember.
taking time to understanding what you are building, where all the different pieces come in is vital for skill for programmer.
It seems a great many people do not recognize the shopping_list as being the list item to pass to the function for this exercise. It is the food list the instructions refer to.
Yes that was an issue. How does Python recognize âfoodâ argument as âshopping_listâ if they are two completely different names, and how were we not supposed to assume otherwise? Would inserting âshopping_listâ as argument instead of âfoodâ yield the same solution? Does Python just recognize them? Because the ambiguity only makes Python even more hard to grasp from these lessons. Iâve moved on but these are some things I havenât understood yet.
The variable food is a local name given to whatever list is passed in. The instructions say it should accept any list so we have to use a different name than the one given to the global object.
Try it and see.
def compute_bill(shopping_list):
Now pass in a list,
print (compute_bill(['apple', 'pear', 'orange']))
Make the list slightly different from the original shopping_list and see if the total is different. If it is, then that means the local variable is not the same object as the global one.
The original list returns 5.5, and the substituted list returns 4.5 so that would indicate there is no conflict between the local variable and global variable. Still, it is more meaningful to the reader if the local variable has a unique name.
def compute_bill(food):
total = 0
for item in food:
total += prices[item]
return total
print compute_bill(shopping_list)
this was accepted, I guess my problem was how to compare the list in the dictionary. I tried to
if prices[key] == item
total += prices[key]
trying to check to see if it was there to prevent an error, which seemed Seemed inevitable because we were asked to make this availableâs list. So I was trying to figure out how to check against the dictionary not coming up with an error. I was trying to call me each item in price checking against what would be in the list. For some reason I wasnât able to donât know howTo make sure that item on a customerâs list was in the prices dictionary. The other issue I had was that if I call the prices dictionary and put item with in it
prices[item]
Mentally this seems wrong because the word item isnt in the dictionary of prices. I would more expect to all these themselves and check against whatâs on the list. For some reason I mustâve missed that if you iterate through a list and use that value term [and dictionary brackets that your comparing or querying or looking for the item in the dictionary. Some reason I didnât see that possibility. But since this worked and I found it somewheres else on the web trying to find the answer to this. I know I donât really understand it could just appreciate someone explaining why and a list work when calling dictionary. Actually as Iâm writing Iâm starting to understand that the parameter is being given and we know that itâs actually prices which is why it will work. So so somebody correct me if Iâm wrong the iteration is being placed into the parameter of the dictionary so that it can call the price of that item. I I guess the problem with that in my mind was that we didnât know what was on the list and Iâm assuming that everyone was this would be different and might not contain items dictionary. In my effort to prevent an error from occurring from a possible list that might contain items not on the dictionary confuse myself wondering about how to check against the key values in the dictionary against the list and thought that I should be pulling keys instead of assuming every list would have items that would have been been in the dictionary. This is my first language that Iâm learning so maybe this is a common thing along other languages. Guessing if weâre going to assume that every list is only going to contain items in the dictionary itâs limited contain that only contain items in the dictionary. I guess the words that it can be used for âany listâ threw me off me off and made me want to prevent errors or lists that donât contain items in the dictionary.