Stocking Out


#1

I am getting error, could someone help me on this.
Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 4 instead of the correct 7

My_program_code

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:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total


#2

One can only guess that the return statement must be inside the loop, so returning on the first iteration.


#3

tried return under the loop as well, still getting the same error, could you share your code?


#4

I am getting the same error. Is there a bug in the tutorial, because I have the same code solution.


#5

Indent properly, it will work just fine
return should be for the for loop


#6

Yes indentation should be given under the function not under the if clause and not under the for loop.
Make sure the return should be given under the function.


#7

def compute_bill (food):
    total = 0
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item] -= 1
    return total

#8

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:
    if stock[item] > 0:
        total += prices[item]

        stock[item] -= 1
        print stock
return total

#9

Does it ask us to print stock or are you just debugging?


#10

that's optional, I just printed it for my understanding to check whether the decrement is happening or not


#12

A post was split to a new topic: Code resulted in 16 instead of the correct 7


#13

A post was split to a new topic: Not getting correct output


#14