Please help can't complete - 12 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):
cost = 0
for item in food:
if stock[item] > 0:
cost += prices[item]
stock[item] -= 1
return cost

compute_bill(shopping_list)

I get an error saying:

Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}


#2

I forgot to mention everything is indented properly I believe I just don't know how to show indentation on the forums. sorry


#3

I think you have your answer right there!


#4

I thought so as well but it won't let me pass


#5

Did you delete that line in particular?


#6

Hi, @joetynan ,

Because your code is not formatted, it is difficult for other users to read and debug it. As code is being posted, you can format it by selecting it, and then by clicking the </> button above the editing area. This will enable us to see important details, such as the indentation and underscores.

Yes, as noted by @zeziba, you have to delete this line to pass Exercise 12: Stocking Out ...

compute_bill(shopping_list)

For the next exercise, you can do this to see results ...

print compute_bill(shopping_list)

#7

So when i use this code it says that it still returns with 0 why is that

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):
    cost = 0
    for item in food:
        if stock[item] > 0:
            cost += prices[item]
            stock[item] -= 1
        else:
            print "error"
        return cost

#8

never mind return cost was just indented one to many times


#9

my code is
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 += prices[item]
stock[item] -= 1

return total

#10

Thanks guys I got it to work it's just I haven't been on in ages. Thanks for all the help :grinning:


#11

maybe the code is
def compute_bill(food):
cost = 0
for item in food:
if stock[item] > 0:
cost += prices[item]
stock[item] -= 1
else:
print "error"
return cost
the code should return the value of cost of the for loop, not for the else


#12

It's counterintuitive - got me too for a while. The error is saying to create the module but don't actually call it.


#13

Hi,

I keep getting this error but my code seems to be okay(or so I think)

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: global name 'sotck' is not defined

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

What could be the problem with the code?


#14

Spelling mistake in the if-statement: 'sotck' instead of 'stock'


#15

Thanks, corrected that but it still brings the error below...

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