Stocking Out


#1


https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096

This is my code. It shows the error message like:

"Oops, try again. calling compute_bill with a list containing 9 bananas resulted in 36 instead of the correct 24"

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 key in food:
        """if stock[key]==0:
            total1=prices[key]*stock[key]
            total=total1+total"""
        if stock[key]>0:
            total=total+prices[key]
            stock[key]==stock[key]-1
    return total
food = ["banana", "orange", "apple","pear"]
compute_bill(food)


#2

stock[key]==stock[key]-1

== is a comparison operator.

If you want to assign stock[key] back to it by changing its value ,Use assignment operator.

Also do not call compute_bill() function. (reason is mentioned in Error message when You run it)


#3

Thanks so much. Calling the compute_bill() function was the only mistake I have done. That's why stock[key]=stock[key]-1 was also not working. And I had to change it to stock[key]==stock[key]-1.

Thanks for solving my problem :slight_smile:

Cheers!


#4

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