- The difference between local and global variables.
- Why returning values from functions is usually better than relying on globals.
- How to pass values between functions to avoid scope-related bugs.
- Local scope: variables defined inside a function that exist only while that function runs.
- Global scope: variables defined outside any function and accessible throughout the module.
Local variable (NameError)
Create a file namedscope.py with this code:
calculate() prints 15, but the print(result) outside the function raises a NameError because result is local to calculate() and doesn’t exist in the global scope.

Global variable (accessible everywhere)
Move the assignment outside the function soresult becomes global:
result because it exists in the module scope.
Local variable shadowing a global variable
If you define a variable with the same name inside a function, the local variable shadows the global one within that function:calculate() you see 10 (the local), while the global result remains 15. Shadowing can be confusing — prefer using local variables for temporary work and keep the global namespace minimal.
Returning values from functions
When you need a value computed inside a function to be used elsewhere, return it and assign it where you call the function:Prefer returning values from functions rather than writing to global state. Returning values makes functions easier to test and prevents unintended interactions between different parts of your program.
Applying scope to the dashboard functions
Apply the same principles to the dashboard: keep logic and intermediate variables local to each function and return or accept values through parameters. Exampleget_mood() that validates input and returns the user’s mood:
get_energy() that validates a numeric energy level and returns it:
show_wellbeing() takes mood and energy as parameters and prints a summary:
show_wellbeing only prints output, so it does not need to return anything. We call it with the inputs it requires.
Goals and completions: passing lists and counts
get_goals() should collect a list of goals and return it:
get_completions() accepts the goals list, asks the user which goals are completed, and returns the completed count:
show_results() prints totals and show_final_message() gives a closing message:
show_final_message uses the number of goals to choose the appropriate final message.
Putting it together in the main flow:
get_goals()returns the list of goals.get_completions()accepts that list and returns the completed count.show_results()andshow_final_message()get the data they need via parameters — no globals required.
Functions with parameters and different arguments
A simple function shows how a parameter receives different arguments:name is a parameter; "Maria", "John", and "Sarah" are arguments you pass in.
You can use multiple parameters:
Common mistakes
Avoid relying on mutable global state for communicating between functions. It leads to hidden dependencies and makes unit testing and reasoning about code much harder.
Recap
- Local variables exist only inside the function that defines them.
- Global variables are visible everywhere, but overusing them leads to fragile code.
- Pass values into functions using parameters, and get results back using
return. - Keep functions focused: give them the inputs they need and return the outputs they produce.
- Minimize side effects by avoiding unnecessary global state.
Links and references
- Python official docs — Namespaces and scope
- PEP 8 — Style Guide for Python Code (readability and design)