Code works in IDE but not in Codecademy - Stocking Out @ Supermarket


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096
<In what way does your code behave incorrectly? Include ALL error messages.>

Oops, try again.
calling compute_bill with a list containing 1 pear, 1 orange and 1 banana resulted in 7.0 instead of the correct 8.5
Actual code:
my_list = {
“banana”: 1,
“apple”: 1,
“orange”: 0,
“pear”: 1
}

<What do you expect to happen instead?> Use the code in the coding window

```python

shopping_list = [“banana”, “apple”,“orange”, “pear”]

stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}
prices = {
“banana”: 4,
“apple”: 2,
“orange”: 1.5,
“pear”: 3
}
my_list = {
“banana”: 1,
“apple”: 1,
“orange”: 0,
“pear”: 1
}

Write your code below!

def compute_bill(food):
total = 0
for key in food:
if stock[key] > 0:
if ((my_list[key] - stock[key]) < 0):
total += my_list[key] * prices[key]
else:
total += stock[key] * prices[key]
return total

print compute_bill(my_list)

<do not remove the three backticks above>

#2

i think you have to simlplfy your function compute_bill
and you forgot to subtract item in stock every time you compute

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

#3

There are several problems.

  1. Your code changes the stock. I would have to comment out
    stock[k]=stock[k]-1

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}

This is the code for stock. As you can see - it contains what is requested above.
stock = {
“banana”: 6,
“apple”: 0,
“orange”: 32,
“pear”: 15
}

  1. I submit my code, no changes, and get different messages every time. Why is that?

Oops, try again.
calling compute_bill with a list containing 3 oranges and 9 bananas resulted in 40.5 instead of the correct 28.5

Oops, try again.
calling compute_bill with a list containing 2 apples, 1 pear, 2 oranges and 7 bananas resulted in 34.0 instead of the correct 30.0

Oops, try again.
calling compute_bill with a list containing 1 apple, 3 pears and 7 bananas resulted in 37 instead of the correct 33