Lesson 12 NEED HELP!


#1

https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3

This code won't work for "A Day at the Supermarket" Section 12:

shopping_list = ["banana", "orange", "apple", "pear"]

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] = stock[item] - 1
    return total

Best of Thanks


#2

Did you try the shorthand minus-equals on this one? Otherwise I don't see any errors.


#3

The code works fine. Try executing again and paste the error message here.


#4

Yep changed the
stock[item] = stock[item] - 1
to
stock[item] -= 1
and it then worked fine!!!!
Question
do you have to always use the "-=" and
"+=" to get code to work in python?


#5

No. This is a special case for the lesson, where it wanted you to use that special syntax. The way you had it works fine in normal Python usage outside of a classroom setting :slight_smile:


#6

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