Making a purchase


#1

I NEED HELP


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 number in food:
        total += number
    return total
print sum(n)


#2

remove this line:

print sum(n)

it doesn't do anything.

So, food is a list containing products (just like shopping_list). You can't add them to total directly. You need to get from product to cost of the product. Maybe you can use your prices dictionary for this?


#3

In your function your variable number represents an index, as part of your for loop, within your dictionaries. The application is trying to add together the prices, i.e. the values, e.g. 4, 2, 1.5, & 3, and not the keys, e.g. banana, apple, orange, and pear. This requires you to define the dictionary, i.e. either the stock or prices dictionary; in this 'Making a Purchase' example you want to pull values from the prices dictionary so you can compute the bill.

Dictionaries contain Key:Value pairs.

To make this work for you:

total += prices[number]

where:
- prices = the desired dictionary
- number = the desired dictionary value

Function

def compute_bill(food):

total = 0

for number in food:
    total += prices[number]

return total

#5

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