Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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.

Aufgabe

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}

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 4
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

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

close

Awesome!

Completion rate improved to 5.26

bookChallenge: Scope Detective

Swipe um das Menü anzuzeigen

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.

Aufgabe

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}

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 4
single

single

some-alt