Why doesn't this work?


#1



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


Oops, try again. compute_bill(['apple']) resulted in a TypeError: unsupported operand type(s) for +=: 'int' and 'str'


I know it has to be like this:

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

And this is how I wrote it:

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 += item #Here's the bug
    return total

By the error I know that it's trying to add an integer with a string, but, what is the correct code doing that the incorrect isn't?


#2

Hi @ahernandezbustos,

Due to this loop header ...

    for item in food:

... item refers to a string within the food list that should serve as a key to the prices dictionary. Therefore, you should do this to access the price data in the dictionary ...

        total += prices[item]

#3

And, what should I write to see the code in action?


#4

Your for loop should be like this, so that you check it, and then reduce it if appropriate ...

    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1

To get the function to execute, you can add this final statement outside the function ...

print compute_bill(shopping_list)

However, Codecademy will complain if you do it now; instead, save that for the next exercise.


#5

And, instead of writing:

print compute_bill(shopping_list)

Why can´t I write:

print compute_bill("banana")

...?

And, how do the shopping list and the keys in stock and prices relate or connect, because it should be the same writing shopping_list or "banana", "orange", "apple", shouldn't it?


#6

You could write ...

print compute_bill(["banana"])

The square brackets are necessary, because whatever you choose to purchase has to be enclosed within a list.

Whatever list you pass to the compute_bill function will get assigned to the function parameter, food, and then get processed in the loop. That is how the shopping_list and the keys in stock and prices get connected. Only keys that are in the stock and prices dictionary should be included in a list that you pass to the function. Otherwise an error will get raised.

Though it would not be an actual error, Codecademy will complain if you call the compute_bill function now with anything at all in the list; that must wait for the next exercise.


#7

Ohhh, now I understand. Thanks :smiley:


#8

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