Invalid syntax in "a day at the supermarket"


I have been trying this code and i keep getting invalid syntax error
also i think there might be an error with the line after for loop

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:
    total += total + item 
  retutn total


this looks a bit off:

total += total + item 

+= is a shorthand, if we write it without shorthand we get:

total = total + total + item

that will make shopping in your market expensive!

also, why would you return item? We want to know the total cost calculated by the function


sorry that was a typo
but i didn’t really get the shorthand thing could you maybe explain it a little bit
And the error that i keep getting is “
retutn total
SyntaxError: invalid syntax”


how could that be a typo? You should copy paste the code you use in the lesson to the forum, to ensure the people helping you get the exact same problem you are facing

did you do this?

well, programmers are lazy, we can write:

x = 5
x = x + 1

but given we are lazy, there are shorthands which saves us time:

x = 5
x += 1

those two codes are the same.


Thanks for explaining that
total += item
But i still keep getting the same error of invalid syntax



I don’t know what the error message is, and you didn’t include the exercise url either to give me quick access to the exercise, so i can run the code there


Sorry i am new here so i don’t really know how to do that but the error that i keep getting is
retutn total
SyntaxError: invalid syntax


check your spelling, you can misspell keywords like return.

also, what is the value of food function parameter? so what will item be? Of what data type? can we just add total and item together?


As i had already seen some discussions so i know that the answer would be total = food[item] but i don’t know what food[item] means


you read a discussion, and then get the wrong answer from the discussion.

but that doesn’t answer my question, food is a function parameter, as such it will get values from argument at function call:

# function deceleration with parameter food
def compute_bill(food):
    print food

# function calls with argument
compute_bill(['pear', 'orange'])

see how the parameter gets its value from the argument at function call (see comments in code)

now that you know food is a list, you should realize that item will be products (of data type string)

string (item) + integer (total) results in an error

how do we get from item to price? Do we have a dictionary which we might be able to use?


We do have a dictionary called prices but what i don’t get is the linking process
If You could just explain how it works like you did in the case of += that would be a great help


yes, we do. and item will contain strings which are valid keys for the dictionary.

so we can use item as key for the dictionary, what value does the dictionary return then? What can we do with this?


Thanks for being so patient
I got it


the most valuable lesson here shouldn’t be the solution, but to learn to break the program down into small steps, so you understand what every step of the program does.

if you have a function parameter, what the possible value of this parameter could be, add a function call if needs be

understand what the values are, what they represent and what they do

programming is mostly problem solving


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