Trouble with "making a purchase" exercise


Making a Purchase

For this exercise I keep receiving the error:

Oops, try again. compute_bill(['apple']) returned 7.5 instead of 2

The following is my code. any suggestions would be greatly appreciated. :slight_smile:

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 shopping_list:
        total = total + prices[item]
    return total


Hi @juskaiser .

Here, you are ignoring the function parameter, food, and accessing the global list, shopping_list, directly ...

for item in shopping_list:

That defeats the ability of the function to handle alternative shopping lists of items that could be passed to it as an argument.


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