Total looks right but I'm stuck at making a purchase


#1

So here is the exercice I’m trying to do :
https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/making-a-purchase

<In what way does your code behave incorrectly? Include ALL error messages.>
I’ve got this error, but I don’t understand what I’m doing wrong :

compute_bill(['apple']) returned 7.5 instead of 2

<What do you expect to happen instead?>
Saying me it’s ok as the total for my shopping list looks right at 7.5 :confused:

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 food in shopping_list:
    total += prices[food]
  return total

#2

food IS your shopping_list (which gets passed into the function)

for item in food:

#3

but it still doesn’t work with
for item in food

This is what I got:
compute_bill(['apple']) resulted in a TypeError: unhashable type: 'list'

I think I don’t understand what “food” is really. With for item in food I don’t understand how my code is supposed to work as there is no way for it to know it should parse my Shopping_list. Am I wrong ?


#4

So … I am currently at this exercise too and how I understood it is:

So if you want to print the whole thing, you would do: print compute_bill(prices)
The dictionary “prices” goes into the argument “food” in the compute_bill function.

So “food” equals “prices”

for a variable called “item” in “food”:
you assign the variable “total” the “item”, which holds the Variables of the dictionary. I don’t understand that part too, because you have to do:

total += prices[item]

and the question is: why do I have to show, that “item” are the variables in “prices”? Because food actually represents prices, right?

Another part that I don’t understand: how does the program know, which Variable it should pick in the dictionary? I mean it could also pick banana, apple orange and pear instead of the variables.

so: tl;dr

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

#5

Thanks. I don’t understand why but at least I can do the next exercice. I don’t understand the line
for item in food
because I understood that the y in for x in y: is always a list or a dictionnary. So I think this is why I’m lost here.

Anyway, thanks for your help :slight_smile:


#6

At the top of the code we start with a list, shopping_list that contains items found in our two dictionaries.

def compute_bill(food):       # food is the local name for the list we pass in
    total = 0
    for item in food:         # item is one of the strings in the list
        total += prices[item]
    return total
print compute_bill(shopping_list)    # the call expression passes the list

#7

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