Not getting the expected result, confused. Please need code to be reviewed


#1

Not getting the expected result, confused. Please need code to be reviewed.>

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

My code does not show any error messages, in fact, it is letting move forward but, it is not printing or returning what it supposed to return which is the "total"

Console should be printing the "total"


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


#2

Did you try invoking the function and printing the return value?


#3

It doesn't look like food is defined.


#4

A post was split to a new topic: Instructions not complete and confusing


#5

It is defined as a local parameter. When we invoke the function, we pass in the shopping list.

print (compute_bill(shopping_list))

#6

A post was merged into an existing topic: Instructions not complete and confusing


#7

A post was merged into an existing topic: Instructions not complete and confusing


#8

Instructions say to ignore whether item is in stock


#9

Hey, its letting you move forward cos the system has tested a list and your program fits to solve it.

Also note that you didnt ask to 'print' total's value but to 'return' it.

That is why its not printing it.

I hope that helps.


#10

Thanks. Yes, it helps a lot!


#11

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