Invalid syntax in "a day at the supermarket"


#1

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

#2

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


#3

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”


#4

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.


#5

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


#6

yes

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


#7

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


#8

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?


#9

Thanks
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


#10

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(shopping_list)
compute_bill(['apple'])
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?


#11

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


#12

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?


#13

Thanks for being so patient
I got it


#14

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


#15

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