#1

History
2 3 / 3 Edit Post
princechima 6 mins

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

`

#2
``````def compute_bill(food):
total = 0
for item in food:
for key in stock:
if key == item:
total += prices[key]
print compute_bill(shopping_list)``````

#3

Thank you!
Let me try it and give feedback!

#4

I ran the code and this is the response:

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

#5

Change it to, Seems the codecademy testing the method with list not dictionary

``````def compute_bill(food):
total = 0
for fkey in food:
for skey in stock:
if fkey == skey and stock[skey] >= 1:
stock[skey] -= 1
total += (1 * prices[skey])

#6

Yeah, it worked!
Thank you very!

#7

This worked! Awesome! Thank You!

#8

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