Stocking out


#1

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

Does anyone know what wrong with the code?
Kept showing: "calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 9 instead of the correct 7"


#2

Hi @andrewcychu ,

See How do I format code in my posts?, so that you can format code for posting.

You have ...

if stock > 0:

EDITED (November 17, 2016):

The value of the name, stock, which represents the entire stock dictionary, is always considered to exceed 0 in Python 2.x. In Python 3.x that comparison cannot be made between a dict and an int.

Instead, compare stock[item] to 0.


#3

Hey! Thanks buddy! First time using this! Good advices!


#4

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