Making a Purchase


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


The code is okay. The console gives me:
"
2
4
None
"
What is correct, however I get pop out error
"Oops, try again. compute_bill(['apple']) resulted in a TypeError: unhashable type: 'list' "


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

print compute_bill("apple")
print compute_bill("banana")

https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


#2

 #   for item in food:

why did you make the loop into a comment? You might might test with function calls which have strings as argument, but the test cases of the exercises are lists, so you will need the loop

using lists also means the customer can buy more then one product


#3

I've changed it, however it still doesn't work.

I thought that it works like that: It have variable "food" which it is looking for in list "shopping_list" so it is looking through the list and it looks for variable "food" when it finds it it adds it's value available in dictionary "prices" into total. When we call for the function we tell it look for e.g. "apple" . So I've changed it like this:

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

In consol it gives me right values, however the pop out says:

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


#4

remove the if condition, item is a string (product) where as food is a list, a string will never equal a list


#5

So for code like this, as you suggested:

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

I have no idea why but it gives me all wanted values multiplied by four. It is very weird cause if you will take a look at whole code:

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

print compute_bill("apple")
print compute_bill("banana")
print compute_bill("orange")
print compute_bill("pear")

There is no sign of it.


#6

You should be looping through food, not prices.


#7

You mean:

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

print compute_bill("apple")
print compute_bill("banana")
print compute_bill("orange")
print compute_bill("pear")

the results are: 10, 24,9.0,12

So i think there is still something wrong


#8

Close, but the key value for prices should be item, not food.


#9

The reason why it was multiplying by four, is because the values within prices were being added to themselves for each item in food. That's not what we wanted.


#10

your should really update your function calls with list arguments:

print compute_bill(["apple"])
print compute_bill(["banana"])
print compute_bill(["orange"])
print compute_bill(["pear"])

this is where you get confused. The exercise will also call the function, and the arguments will also be of type list

having a list, also means the customer can buy multiply products:

print compute_bill(shopping_list)

and that is also why we have a loop, to get the items from food list

lists can't be used as key to access a dictionary (prices)


#11

Wow thanks, a lot. It is working


#12