11. Making a purchase


#1


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

Giving me the error compute_bill (['apple']) resulted in keyerror:1
Also I don't really understand why I put the 1 in total=total+prices[1], I got that from another thread. My code seems to match the solution in that thread but it isn't running.


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=total+prices[1]
    return total


#2

This is my solution

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

As the exercise goes, the function is supposed to add up your shopping list into the total.

The "each" in my code refers to the argument in the shopping_list list (at the moment is not inserted yet) the

total += prices [each]

refers to the global dictionary "prices", where the "each" becomes a key to the value for each item.


#3

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