Making a Purchase


```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 += item
    return total


I want to return total, but it pops up an error message. I cant figure out what is wrong with my code. Any help plz.


food (your function parameter) is a list of items, you want to buy. Then you loop over the list to get the items, but to add to total, you need to convert item to price, maybe you can use your prices dictionary?


can you gie a sample ocde please? i have try this but its getting me worng!


no, that is a violation of the guidelines. Please make a new topic, fill in the template so we have all the information needed to help you


This is the message error i got when i run my code

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


because item (as you can read my explanation) contains string name of products ('apple' for example), so you need to convert from item to price, how to achieve this?


Hi @nouhahmed,

It may help for you to review Python Lists and Dictionaries 10. This Next Part is Key.

There, you will find information regarding how to use a key to access an item in a dictionary.


if you still need it(help) try prices[item];


A post was split to a new topic: Making a purchase


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