Do variables within functions also function outside the functions?


#1

A day at the supermarket, lists + functions: https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/lists--functions?action=resume_content_item
for the example given in the explanation I noticed that the defined variable within the function was not reused to print the value.
Feeling like this was a waste of NameSpace I tried to directly print the variable total, but was unsuccessful:
https://gyazo.com/c4e557e600bd01daf52bf9c150fa64bc
(note: I did remove the variable which was equated to the output here and instead directly print the result of the function)
Am I correct to note that any variables defined within the function will non conflict with variables defined outside the function (and is that a two way street, do variables defined outside the function also not conflict with any variables redefined within them?).
That’s all for today, have a good day!


#2

yep, this is called scope. total has a local scope, it only exist within the function

lotto on the other hand has a global scope.


#3

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