Challenge: 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.
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_countercorrectly updates the globalcounter. - Make sure
print_messageprints 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}
Løsning
Tak for dine kommentarer!
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 5.26
Challenge: Scope Detective
Stryg for at vise menuen
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.
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_countercorrectly updates the globalcounter. - Make sure
print_messageprints 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}
Løsning
Tak for dine kommentarer!
single