Skip to main content
Welcome back. When we refactored the dashboard into smaller functions, one immediate benefit was clearer control over where variables live — this is called scope. Understanding scope helps you avoid bugs, write testable code, and keep functions predictable. In this lesson you’ll learn:
  • 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.
Quick definitions:
  • 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.
Let’s see these concepts in practice.

Local variable (NameError)

Create a file named scope.py with this code:
What happens when you run this? The call to 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.
A code editor window (VS Code) showing a small Python script that prints a local variable and a terminal error (NameError: name 'result' is not defined). A presenter wearing a KodeKloud shirt is visible gesturing in the bottom-right corner.
Example run (illustrative):
Takeaway: local variables exist only while their function executes.

Global variable (accessible everywhere)

Move the assignment outside the function so result becomes global:
Output:
Now the function can read the 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:
Output:
Inside 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:
Output:
This pattern keeps temporary variables inside the function and only exposes what the caller needs.
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. Example get_mood() that validates input and returns the user’s mood:
Example get_energy() that validates a numeric energy level and returns it:
Both functions return values so the main program can collect them and pass them to other functions. show_wellbeing() takes mood and energy as parameters and prints a summary:
In the main program you collect values and pass them in:
Note: 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:
Note: show_final_message uses the number of goals to choose the appropriate final message. Putting it together in the main flow:
Make sure:
  • get_goals() returns the list of goals.
  • get_completions() accepts that list and returns the completed count.
  • show_results() and show_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:
Parameters define what a function expects; arguments are the actual values you pass.

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.
A follow-up lesson will finish polishing the dashboard and encourage you to adapt it to your own needs.

Watch Video