There are few problems with your function you created ,I will point them.
total = 0
for food in shopping_list:
I will go explaining line by line.
1.You created a function called
2.You pass in a parameter called
food , This
food parameter is a list here .
It can be any list(with the context of the code,It can be list of fruits,any list)
Remember the parameter inside a function are local variables and they're
replcaed by the value when you call your function and pass them.
Two concepts to remember.
Global variable - the variable defined outside of function is called global variables and they can be accessed any part of the program.
Local variable -- The variables defined inside the function are used to be in local scope and they exist till you call your function and pass it some value,all the variables will be replaced by the value you pass. local variables exist till function gets called and vanish when call gets finished.
return 2 * x
I defined a function double and passed it a parameter
Now lets say I defined a variable outside of function..
z = 3 #global variable
When I call the function like this..
x is replaced by the value of
z and calculation is done on
and it return 6.
I hope it clears your doubt.
3.You start looping over
shopping_list using iteration variable
Two mistakes here..
A.You already used
food variable as your function's parameter so you need to use some other iteration variable other than
food. (EDIT : It will create confusion between which is function's parameter or which is iteration variable.)
B.You do not have to iterate over just
shopping_list list, You have to iterate over your general list,your parameter
You have to use this..
5.last line is okay.
Make changes as I mentioned and tell me what you get!