Making a Purchase


#1



This is regarding the exercise 'making a purchase' under 'A day at the supermarket'


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


The error message says "Oops, try again. compute_bill(['apple']) returned 10.5 instead of 2 "
I tried a work around by setting the total at -8.5 here's what happens "Oops, try again. compute_bill(['banana', 'apple', 'orange', 'pear']) returned 2.0 instead of 10.5 "


I suppose I expected all of this to work but , clearly it isnt


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


I think I'm doing something really stupid but , any help is appreciated


#2

why do you loop over stock?

we want to calculate to how much it would customer to buy certain products (by supplying a list as argument at function call), not how much 1 product of each in stock would cost

remember: arguments at function call get passed into the function parameter (food)


#3

I don't understand , does it really matter if i loop over stock or prices ?
My understanding is that

total = total + price(item)

regardless of the dictionary its looping through would only take the keys from the prices dictionary.
I think I'm stumbling upon a bigger problem in my understanding here :
So if I loop over the stock it would add the price of the item as it is in the stock. If looping over prices it would do the same .As long as I call the correct dictionary , here the price. the only difference being that if the order of the items were different in stock and prices it would add them up in a different order
but arithmetically it should be the same as (a+b+c)=(c+b+a)

edit: I tried looping over prices and still got the same error. I'm quite helplessly lost now


#4

I'm so sorry , I understand now , i'm looping through the stock which ends up adding everything there, the code must read

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

#5

yes, you understand as well? Now the customer can buy different products (which is coded as a function call with a list of products the customer wants to buy as argument)


#6

Can someone offer some guidance as to where my code is going wrong for this lesson? I feel like i'm running the loop over the right list but it keeps returning the error "Oops, try again. compute_bill(['apple']) returned 7.5 instead of 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
}

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

#7

no, you are not:

lets add the function call:

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

print compute_bill(['apple'])

that is a bit unfair, customer wants to buy a apple, gets charged for an apple, banana and orange


#8

Code does the calculation, I get the correct result but I also get the "TypeError: list indices must be integers, not str".

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

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

What am I doing wrong?


#9

here:

print compute_bill(prices)

you supply prices as argument on function call, however this should be a list, this list represents the products customer wants to buy


#10

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