Stocking Out exercise


#1



Replace this line with your code.


#2

**Could I please get feedback on my code below, when I run it I get the following error message, "calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 'apple' instead of the correct 7"? Why is error coming up? On the screen, the output I am getting is 'none' as well. **

Write your code below!

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 produce_item in food:
if stock[produce_item] > 0:
total = total + prices[produce_item]
stock[produce_item] = stock[produce_item] - 1
else:
return produce_item
return total


#3

Hi @yochanan_30,

I can't tell whether your code has been indented wrongly since it is typed in without the proper indentation. But reading your code, here are some comments I have:

  1. The else statement is unnecessary, remove it. Other than that, your code seems to be written correctly. I think your error might be due to inappropriate indentations, like putting the last return line under the else statement etc, but I really can't tell unless you quote your code properly :stuck_out_tongue: can you please repost your code?

  2. instead of writing statements like "total = total + prices[produce_item]", you can just use total += [produce_item].

Even if your code is correct, I think nothing will show on the screen since the code does not include any print statements :slight_smile:


#4

I have a similar problem, this is my code properly shown:

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

print compute_bill(shopping_list)`

This is the error 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}

But I am asked to change the stock! What am I doing wrong?


#5

**print compute_bill(shopping_list)**

please remove this line from your code , it is worked for me...


#6

Your code looks fine but there are a few more lines that are unnecessary. Delete line "print food" and 'print compute_bill(shopping_list)' Then try again. It should work fine


#7

Hi guys i get it. My code works:

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


#8

Thanks. It was that simple.


#9

Hello! My code in this exercise is:

# Write your code below!
def compute_bill(food):
    total = 0
    for fruit in food:
        if stock[fruit] > 0:
            total += prices[fruit]
            stock[fruit] -=  1
    return total    
    print total

compute_bill(shopping_list)

But i get the error:
Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock!

If i detele "compute_bill(shopping_list)" and commit then there is no error. But i think it`s wrong. How we could get shopping_list in compute_bill? May be this unit-test is wrong? I spent an hour looking for an error which is not. But I figured it all out :slight_smile:


#10

Error says not t call compute_bill. That's why when you delete it, you don't get the error. Error messages are important, please read them carefully!

Codecademy has its own scripts to test your code in the back. With you calling the function, you are also affecting its test scripts.


#11

Thank you! It would be more understandable if this were specified in the instruction, since the call to this function remains with the previous exercise.

P.S. And a lot of questions on this exercise confirm this :wink: