11. Making a purchase - what's wrong with this code?


#1

Hi,

I'm stuck on the 'making a purchase' lesson of the 'day at the supermarket' section:
https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096

As far as I can tell, my code is doing what's required, but I get the following error:

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

I think my code is looping through each value in the shopping_list list and finding the price of each item in the prices dictionary. Each item found is added to a running total and the final total is printed out at the end. I've added extra print commands within the loop to check each item as it's found, that it's picking up the correct price and that the total is stacking up as expected and all looks to be well. So why the error? :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:
        print food
        print prices[food]
        total = total + prices[food]
        print total
    return total

print compute_bill(shopping_list)

#2

The error is a result of looping through the shopping_list instead of through food, which is being taken as input to the function. I had some confusion with this as well, but if you read through the instructions carefully you will note that it asks you to create a function that takes a list as 'food' and then loop through the inputted list and gather the prices.


#3

Ahh, okay, that makes sense. Thanks :slight_smile:

I guess this leads me to a further question. Is it a case that what I've done is just plain wrong in Python coding terms, or is it just incorrect for this particular Codecademy exercise?


#4

First off, I am not an expert. That being said I believe what your doing is bad practice in Python coding terms as well as this particular exercise. The main problem is that your function takes an input and then doesn't do anything it. You could change your function so that it doesn't take any input and then iterates through shopping_list, and while it would work just fine, it would be very impractical and inefficient. The best way to do it is to take a list as input, iterate through the list that was inputted and then return the total.


#5

I don't understand my error either. Take a look please.
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 thing in food:
total += food[thing]
return total
print compute_bill(prices)
it gives 10.5 as the answer but it gives an error saying "Oops, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str"


#6

In your for loop where it says
total += food[thing], what that is doing is going through food, which is what is being inputted into the function, and iterating through it. So if ['apple'] is the input to the function, the for loop will on the first time add the first item ("apple") to the total.

What it should say is
total += prices[thing]


#7

wow! it worked. But i still don't get it. Why didn't total += food[thing] work? I gave food as a general input. when i wanted it to compute through the prices list i gave it compute_bill(prices) so that it goes through the entire loop just with the replacement of prices instead of food.


#8

When you have def compute_bill(food) food is a variable that equals the value of whatever is inputted. So when you run compute_bill(prices) food equals the prices dictionary.

How your for loop was setup meant that it would iterate through food and add the value of each item in food to the total. This works for prices because each value is a price of a food item, but if we were to run compute_bill(["apple", "orange"]) it would set food equal to ["apple", "orange"] and then try and add food[0] (which is "apple") and food[1] (which is "orange") to the total. This obviously won't work.

However, if our for loop has
total += prices[thing] then it will go through and pull the price for "apple" and add it to the total and then grab the price of "orange" and add that to the total. This means that we can run compute_bill with any list of foods and get the total bill.


#9

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


#10

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

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 x in food:
if(stock[x]!=0):
total += prices[x]
return total
print (compute_bill(shopping_list))

Error says calling compute_bill with a list containing 1 pear, 2 oranges and 9 bananas resulted in 42.0 instead of the correct 30.0 , why is it inputting 9 bananas when stock is 6


#11

The compute_bill function is only looking at a list for input and then the price of each item. It doesn't care what the stock level is of that item. All the function should do is take a list of items and calculate the total with the assumption that the quantity being purchased is in stock. Obviously for a real life situation you would want it to check if it's in stock, but for this excersise don't worry about the stock level.


#12

Thank you so much for your help =)


#13

Can anyone help with this? At least to me this:
def compute_bill(food) :
total = 0
for item in food :
total += prices(item)
return total
print compute_bill(prices)

looks exactly the same as this:

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

and for some reason the second one works and the first doesn't. Can anyone explain why?

Thank you!


#14

In the first one the for loop is running total += prices(item) where are the second one is running total += prices[x]

When an element of a list, dictionary, or string is being called via index, brackets need to be used and not parentheses.


#15

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

print compute_bill(shopping_list)


#16

Thank you so much. It would have driven me crazy trying to find that. Really need to keep track of that small stuff better


#17

Somehow I cannot get the right answer either. Can anyone find out what's wrong with this 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
}

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

Keeps coming up with:
" Oops, try again.
compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5"


#18

I just ran your code and assuming you are indenting properly it should run fine.


#19

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