11. Making a Purchase


Oops, try again.
compute_bill(['banana', 'apple', 'orange', 'pear']) returned 4 instead of 10.5

it keeps calculating it wrong. I have wasted a f'n hour on this s*** already and cant see the f*** problem.

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 itemz in food:
        total = total + prices[itemz]
        return total

to test it, I used:

n = ["banana", "orange", "apple"]

print compute_bill(n)

for itemz in food:
    total = total + prices[itemz]
return total


i think you should return the total after the loop ends , because if we return something that mean the end of the function


In other words,

    for itemz in food:
        total = total + prices[itemz]
    return total


Just don't include the return statement in your for loop.


I think you guys completely misunderstood my own reply/fix to this problem :slight_smile:

"return total" was too far right, removed 4 spaces and it started to work.


Which basically means the early responders did not see your post when they posted. This is essentially an asked and answered topic, so doomed to deletion in the course of things.


I can't understand. The result is right: 4+2+1.5+3 = 10.5
So Total returns correct number.

But the System shows me: Oops, try again. compute_bill(['apple']) resulted in a TypeError: list indices must be integers, not str (and it's not a console; it's a hint)

But why it tryes to put ['apple'] in the agrument of function? I don't understand... Help me.


Here you see what happens when i submit code. What's a problem?


    total += prices[item]

food is the list that we pass in to be evaluated.


Why is my interpreter showing this error this error.


We are given a shopping_list to pass in to the compute_bill() function. We don't have to pass in any of the global objects, prices or stock. The function can see and access them already.

    total += prices[item]