Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Understanding Variable Scope | Variable and Assignment Mistakes
Common Python Mistakes and How to Fix Them

bookUnderstanding Variable Scope

12345678
count = 10 def increment(): count = count + 1 # Tries to increment the global 'count' print("Inside function:", count) increment() print("Outside function:", count)
copy

Variable scope in Python determines where a variable can be accessed or modified. In the code above, there is a variable count defined outside the function, making it a global variable. Inside the increment function, the code tries to increment count without declaring it as global. Python treats any assignment to a variable name inside a function as creating a new local variable unless you explicitly declare it as global using the global keyword. Because of this, count = count + 1 inside the function tries to use a local count that has not been initialized, resulting in an UnboundLocalError. To modify the global variable inside a function, you must declare it as global within the function.

1. Which statement about variable scope in Python is correct?

2. Drag and drop the global keyword to the correct place in the function to allow modifying the global variable.

question mark

Which statement about variable scope in Python is correct?

Select the correct answer

question-icon

Drag and drop the global keyword to the correct place in the function to allow modifying the global variable.

total = 5 def add_one(): total = total + 1 print("Inside function:", total) add_one() print("Outside function:", total)
Inside function: 6
Outside function: 6

Click or drag`n`drop items and fill in the blanks

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show me how to fix the error in the code?

What happens if I use the `global` keyword inside the function?

Can you explain the difference between local and global variables in more detail?

Awesome!

Completion rate improved to 5.26

bookUnderstanding Variable Scope

Swipe um das Menü anzuzeigen

12345678
count = 10 def increment(): count = count + 1 # Tries to increment the global 'count' print("Inside function:", count) increment() print("Outside function:", count)
copy

Variable scope in Python determines where a variable can be accessed or modified. In the code above, there is a variable count defined outside the function, making it a global variable. Inside the increment function, the code tries to increment count without declaring it as global. Python treats any assignment to a variable name inside a function as creating a new local variable unless you explicitly declare it as global using the global keyword. Because of this, count = count + 1 inside the function tries to use a local count that has not been initialized, resulting in an UnboundLocalError. To modify the global variable inside a function, you must declare it as global within the function.

1. Which statement about variable scope in Python is correct?

2. Drag and drop the global keyword to the correct place in the function to allow modifying the global variable.

question mark

Which statement about variable scope in Python is correct?

Select the correct answer

question-icon

Drag and drop the global keyword to the correct place in the function to allow modifying the global variable.

total = 5 def add_one(): total = total + 1 print("Inside function:", total) add_one() print("Outside function:", total)
Inside function: 6
Outside function: 6

Click or drag`n`drop items and fill in the blanks

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
some-alt