A DAY AT THE SUPERMARKET Making a Purchase


#1

here is my code.
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 items in shopping_list:
total= total + prices[key]
return total
the error states File “python”, line 22
SyntaxError: ‘return’ outside function
any tips anyone?


#2

SyntaxError: ‘return’ outside function

return only makes sense inside function. so make sure the return statement is inside of that function.
check the indentation

and here

total= total + prices[key]

where does key come from?


#3

ive tried the indentation a lot and it hasnt worked. but should i try prices[items]


#4

to show the indentation here do the following

  • select the whole code
  • click on the </> above

you can edit your topic

yes ,you should :slight_smile: because you used items as a placeholder for the loop to go through the elements of the shopping_list


#5

when i select it and click the buttons it replaces the code with the symbols. Do i have to do something else with them?


#6

Have a look at this topic


#7

I still don’t understand. I tried the back ticks and it said that the prices dictionary appears to be missing.
and i tried indenting and it said indenting error so i brought it back but then it gave me the same error.


#8

def compute_bill(food):
total = 0
for ab in food:
total = total + prices[ab]
return total
bro use this one code. This will do it.


#9

i got it before but thank you for helping though.


#10

Do have any questions now?


#11

I try that code but it does not work
the result is wrong
any suggestion?


#12

did you indent? just send us the code and the error please


#13

The program is worked, seems like the problem in the indent
Thanks for the code so i can pass this level


#14

did you keep the space before return? Try doing that.


#15

Thanks
The problem already fixed


#16

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