A Day at the Supermarket: Making a purchase


#1



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


File "python", line 22
SyntaxError: 'return' outside function


I need it to find the total of the items in the shopping list


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 food in shopping_list:
   total += prices(n)
return total
print compute_bill


#2

Check your indentation levels for the for loop and return statement. Remember that all the code inside a function needs to be indented and not on the same level as the def function_name():


#3

Check the indents again.


#4

Hi Chipjumper,

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 fruit in food:
if stock[fruit] > 0:
total = total + prices[fruit]
stock[fruit] = stock[fruit]-1
return total
print compute_bill(shopping_list)

Could you tell me the error in this
its throwing "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}"


#5

Make sure to not call compute_bill since it changes the stock!

You simply need to delete this line:

print compute_bill(shopping_list)

#6

The error message is pretty self explanatory, don't call the computing bill. Delete this line and you should be good assuming your indents are correct.


#7

why this code isn't working

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 fruit in food:
total += prices[fruit]
return total


#8

Makse sure to put " return total" outside of the for , it should work


#9

Did you indent right? I can't tell unless I know what your error message is. Can you display your error message?


#10

File "python", line 21
SyntaxError: can't assign to operator

def compute_bill(food):
total = 0
for item in prices:
total + prices(item) = total
return total


#11

When accessing a list, dictionary, or string through an index we use brackets instead of parentheses. Currently in total + prices(item) = total you are using parentheses.

Also, you should flip the order of that. How you have it now you are setting the value of total + prices[item] to be the same as total. What you want to do is set the value of total equal to the value of total + prices[items]


#12

I did as you said and that worked but now it's returning 10.5 instead of 2...

Write your code below!

def compute_bill(food):
total = 0
for item in prices:
total = total + prices[item]
return total


#13

Your code is looking through prices and adding up the price of each item, but the instructions want a function that will go through a list that is inputted to the function and grab those prices. Currently your function is taking "food" as input, so we can iterate through that and grab the prices of all the items in "food".


#14

I dont understand how to do that


#15

In your for loop change prices to food so that we go through the list that is inputted to the function.


#16

thank you so much that worked perfectly!!


#17

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