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

bookChallenge: Scope Detective

Variable scope issues can cause some of the most confusing bugs in Python programs. When you use the same variable name inside and outside a function, you might expect changes to affect the same variable, but Python treats variables differently depending on where and how they are assigned. If you accidentally create a local variable when you meant to update a global one, or if you try to use a variable before it is properly defined, your code may not work as intended. Being able to spot and fix these problems is an essential skill for any Python programmer.

Task

Swipe to start coding

You are given a script with functions that attempt to update and access variables defined both inside and outside their scope. Your goal is to fix the code so that each function correctly accesses or modifies the intended variable.

  • Adjust the variable declarations or use the appropriate keywords so that increment_counter correctly updates the global counter.
  • Make sure print_message prints the message defined outside the function, not a local variable.
  • Ensure the output matches the intended behavior.

Templatized output strings:

  • For printing the counter: {counter}
  • For printing the message: {message}

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 5.26

bookChallenge: Scope Detective

Swipe to show menu

Variable scope issues can cause some of the most confusing bugs in Python programs. When you use the same variable name inside and outside a function, you might expect changes to affect the same variable, but Python treats variables differently depending on where and how they are assigned. If you accidentally create a local variable when you meant to update a global one, or if you try to use a variable before it is properly defined, your code may not work as intended. Being able to spot and fix these problems is an essential skill for any Python programmer.

Task

Swipe to start coding

You are given a script with functions that attempt to update and access variables defined both inside and outside their scope. Your goal is to fix the code so that each function correctly accesses or modifies the intended variable.

  • Adjust the variable declarations or use the appropriate keywords so that increment_counter correctly updates the global counter.
  • Make sure print_message prints the message defined outside the function, not a local variable.
  • Ensure the output matches the intended behavior.

Templatized output strings:

  • For printing the counter: {counter}
  • For printing the message: {message}

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

some-alt