Making a Purchase


#1



Plese help with this exercise, don't know where i mistaken in my code

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


Oops, try again. compute_bill(['apple']) resulted in a TypeError: 'dict' object is not callable


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 number in food:
        total += prices(number) 
    return total


#2

I solve this problem. Sorry

total += prices[number]


#3

I have an understanding-what-the-code-does question:

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): # function compute_bill takes an argument food as input
    total = 0 #starting value
    for piece in food: # for each item in food add the price to total
        if stock[piece] > 0:
            total = total + prices[piece]
            stock[piece] -= 1
    return total

print compute_bill(shopping_list) #test the function with shopping list = 5.5
print stock #test if the new stock is right = "banana": 5,"apple": 0,"orange": 31,"pear": 15
print total

theoretical I understand what the function does/should do - but the total output is always 117.0, why? Shouldn't it the same like compute_bill(shopping_list) if I run it with this list?


#4

I have the same question as above. I figured out how to do the code correctly, but I don't understand how this connects to the shopping list at all?

I originally set up the code to run the function over shopping list, which returned many errors even though it completely made sense. We wanted to know the prices of the items in the shopping list.

Eventually I ran the function over item and food, but how does it know to connect all of that to the shopping list? This makes no sense to me.


#5

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