A day at the supermarket: Making a Purchase


#1

Soooooooo....
When I try to run my shopping list-bill adding machine, it gives a mysterious 7.5 instead of my expected output of 2...

Here is the link to the one I am stuck on:
https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/2?curriculum_id=4f89dab3d788890003000096


So once again, when I run the code, codeacademy tells me that:
Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2

and I have no idea where it got 7.5 from, I expected it to give me 2 (ie 0 + 2).

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

And idk why it don't work!

Thanks in advance for your kind help!


#2

Here look at my code, I will explain what you did wrong:

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[item]
    return total

Look at your Compute_bill function and compare it with me. You should put for item in food.

Try that. It works right? Message me if you need anymore help!

~Candy :smiley:


#3

Thanks Candy, works great!


#4

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