12. Stocking out


#1



https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096


I do not understand why this code does not work

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in None instead of the correct 7


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


#2

You're not returning total after loop is over and amount is saved in total


#3

Ok, thanks it now works


#4

Cheers!!!.. :thumbsup:


#5

I am struggling with this exercise as well. I have tried several variations with the lists to try and get the function to work.

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 x in food:
if stock > 0:
total += prices
stock = stock -1
return total


#6

Write your code below!

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

return total

try this.....


#7

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