Making a Purchase


#1




```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
        ```
    



#2

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.


#3

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?


#4

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


#5

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


#6

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'


#7

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?


#8

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.


#13

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


#14

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


#15

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