12/13 Stocking Out. Cannot finish this task


#1

How can I make my code working and decrement my stock of bananas to work (and finish the excersise?)

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


#2

Hey, I did things a bit differently and checked for stock as part of an if else loop. Works great. I think you'd need to validate in the loop, otherwise you'd never run the loop if the first item isn't in stock (I'm looking at you apples!). That configuration would also not verify stock after the first item is validated, as the stock check is outside of the loop.

Might be able to do something more refined with the else statement, but this got the job done.

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


#3

This did not help me any tips?

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


#4

You're not handling the cases that don't match your if statement. That's why I have the else return right before the return total. Without that the apple or any other food that hits 0 will not be handled correctly.


#5

Interesting, I ran the test without the else statement and it works just fine.

Based on that, I'd just make sure the indent is right on your code. Should look like this:

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

Maybe you have an issue with the return being indented too far or not far enough? Too far would make sense, as I don't think that'll create a console error. Not far enough generates an error though.


#6

Thank you codyroche, yes it was fault of indent (not only but even when I have corrected mistakes) I cant even imagine how big problems people would have if they dont watch their indent. Thanks a lot!


#7

You solved my problem, thank you!


#8

Solved my problem too! Thanks!


#9

You can use this also !

Write your code below!

def compute_bill(food):
total=0
for i in food:
total += prices[i]
return total


#10

The error I keep getting is telling me not to call the compute_bill function. I have copied your code exactly but keep getting the same error. Any suggestions.

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}


#11

The error checking that runs during the submit seems to check values after running the function. You're likely calling compute_bill after the code then, possibly from when you were troubleshooting the code. Remove any calls to the function you've got in the code and it should work.

If you're still getting the error respond with all the lines of code on the page, not just the function, and I'm sure one of us will be able to see why you're getting an error.


#12

Thanks for replying yesterday. I double checked the code and do not see where I am calling the function. This is what I have.

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

I'm obviously missing something.


#13

Hey cbs301, I think I see the issue. You've got the stock defined as strings! Remove the single quotes from them and you should be good to go.


#14

Wow such a simple problem when you "see" it. Thanks so much. It worked.


#15

Yep, makes you wonder how anyone gets a large program to run smoothly! Can't imagine looking for issues like this in thousands of lines of code!


#16

Not exactly. It's a good start, but there isn't any inventory checking or management in your code.

You still need code to ensure the stock isn't exhausted for an item (stock must be > 0 for it) and you need to decrement the stock as you buy it. Otherwise, you'll be able to buy an apple (not in stock from the start) or 50 bananas (more than the total stock of bananas).


#17

Write

def compute_bill(food):
total=0
for item in food:
for x in stock:
if item==x and stock[x]>0:
if food[item] > stock[x]:
total += prices[x] * stock[x]
#stock[x]=0
else:
total += prices[x] * food[item]
#stock[x] -= food[item]

return total

total = compute_bill(shopping_list)
print total


#18

This one wroked for me. Indentation appears to be the issue


#20

this is my code it isn't working. I get this as an error=Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 2 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
}

Write your code below!

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


#21

you might have forgotten to do
stock[item] -= 1