Bug? ( stock doesn't look quite right!..)


#1

Hi. I dont understand what am i doing wrong.

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


#2

I function ends when the return keyword is reached. Is return total placed correctly in your code?


#3

defenetly, right under for


#4

Could you post your new code in Preformatted text? Thanks!


#5

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

#6

Hmmm... I tested your code and it works just fine :thinking:. You're not getting any other errors, correct? Can I see your full code please, just to be sure? Thanks :slight_smile:


#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 i in food:
        if stock[i] > 0:
            total += prices[i]
            stock[i] -= 1
    return total
    
print compute_bill(prices)

#8

i have only this one error
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}


#9

Just remove this line. Codecademy doesn't want you to call any functions when printing at the time. So just don't print anything basically.


#10

i recieved the same error :slight_smile:


#11

Can I see your new code again? :thinking:


#12

oh wait, got it, thanks alot!


#13

Sure, no problem :slight_smile: :thumbsup:


#14

I consider this a bug since it's an error given by calling the function. Your code is that same from the previous exercise yet we are supposed to magically know to remove the function call. The error comes because codecademy is checking the stock to determine if you have the correct number in stock, but as soon as you call the function the stock changes just as they request.


#15

I'm pretty sure your code is correct. However, Codeacademy doesn't wan't you to factor in stock at this point. So get rid of any lines containing the stock list. Otherwise, your code should work.

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

#16

If you get the following message ...

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}

... it is from Codecademy. It does not mean that anything is wrong with your code as far as the Python interpreter is concerned. To pass the exercise, however, you must abide by Codecademy's wishes.

The message is a consequence of a design decision that the author of the course made when composing the Stocking Out exercise. Essentially, the author decided that the user should not call the compute_bill function in that exercise. Nevertheless it is reasonable, and good protocol, for programmers to test functions when they write them. Although the instructions do not ask you to do so, you can feel free to call the compute_bill function in the final exercise, Let's Check Out!, without any problem.

Thanks for shopping at the Codecademy supermarket! :laughing:


#17

def compute_bill(food):
total = 0
for item in food:
if stock[item]>0:
total = total + prices[item]
stock[item] = stock[item]-1
return total
#simplified for better understanding. Thanks!


#18

I get this error “Oops, try again. calling compute_bill with a list containing 2 oranges and 9 bananas resulted in 39.0 instead of the correct 27.0”

My code works fine and I followed the instructor but still get above error with this code.

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
return total
print compute_bill(shopping_list)


#19

You have commented the line to reduce the stock


#20

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