"Making a purchase" doesn't seem to instruct us correctly -- help!


Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2

What I don't understand about this exercise is that the instructions ask, very clearly, for me to add the price FOR EACH ITEM TO THE TOTAL (not SOME given item -- EACH ITEM). If what this exercise wants me to do is provide the price value (0 + price) of one particular item, such as apple, then it would be much more efficient to just call the value of apple from the dictionary. Why would we create a function for adding values, only to call the value of ONE ITEM from it. For this reason, I see 7.5 as the answer I want (the amount one needs to pay to get one of each item on the shopping list). The use of the apple in particular makes it clear that quantity doesn't matter, as there are no apples. Seems clear to me that the instructions want us to provide a function that adds the prices of each list item.
I must not understand what this exercise is for. I would appreciate any help. Thank you.

shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3

# Write your code below!
def compute_bill(food):
    total = 0
    for food in shopping_list:
        total = total + prices[food]
    return total


The compute_bill function should accept a list of items, look up the prices for them and sum up those prices.

Currently your function ignores the argument that is given to it. You can tell it "elephant" and it'll still produce the same result.

Your function fails on a list containing one item, that doesn't mean that it should only handle lists of one item, it could be any amount, including zero


I wish i could say it makes sense to me now, but I'm more confused. It would be helpful if were more specific. How does it ignore the argument when the argument can be found within the function?


def compute_bill(food):
total = 0
for item in food:
total = total + prices[item]
return total
print total

I know this is the correct code.
Why is there a for statement for food when food is not a list?
How does prices[item] know what item is?


It never uses that information. In no place in your code do you read that value.

What makes you say it's not a list?
What input should the function be accepting?

How specific I get is up to how specific your questions are btw.
Similarly, if you don't demonstrate having considered the information you have, you'll probably get a question in response.
This has to do with that you gotta establish the problems before you solve them. If you skip that part, then that's exactly what stops you from finding the answer


I can assure you I have considered the information and I appreciate you commenting, but keep in mind Im still learning and maybe not as adept at this as you.


I see food in my function at total = total + prices[food]


And the reason I say food isn't a list is because we haven't made a list called food, so it's not clear to me that we are supposed to be working with food as a list.


"Currently your function ignores the argument that is given to it." If I didn't realize that I wasn't putting the argument in the function when I wrote it, I probably can't figure out how to do so with that statement.


It says for each item in the food list, but what food list?
Is this list created simply because we made food an argument in compute_bill?


The instructions describe what input your function should expect.

Not saying that you must be able to see everything from the start, I'm saying that you may not get the answer you expect because you forgot to consider something and you'd get a question about that something instead of the answer you expect.
Point is that I won't run ahead of you in reasoning about the problem. If you're stuck with something on the way, then I'll help with that particular thing and then you'd keep going from there.
If you get stuck a lot of times, then that's not saying that you should get carried all the way, it's saying that there are multiple things to address.
Solving the problem isn't at all what's interesting - what's interesting is learning the skills required to solve it.

No but that's where you went wrong so you'd have to reconsider the situation with that information. And it might actually be enough. If it's not enough, then what's the next thing that's missing? I can't really guess, but you gotta start by reconsidering that.
Since the function has some kind of input that is supposed to be used in order to get the correct output, you'll then have to consider what you were supposed to do with that input. You can do a lot with that information.

The description describe the function in terms of what the input and output is. You can do whatever you want to get from the input to the output.


You'd start by understanding what the function should do.
Then do it manually a few times, see if the result is reasonable.

Then, consider what you did manually, and write those actions down as code.
If that code gets a different result, then find out what it did differently from what you did manually, for example by printing out what's going on at each step, sort of like if the code was explaining to you what it's doing.

If the program does what you want it to do, but still isn't right, then that's saying that you got the instructions wrong and would need to revisit them and see if there's another way to interpret them. You'd also consider what the correct answer is said to be, such as 2 for ['apple']


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