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

Tâche

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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you give examples of common variable scope mistakes in Python?

How can I avoid variable scope issues in my own code?

What are the differences between local and global variables in Python?

close

Awesome!

Completion rate improved to 5.26

bookChallenge: Scope Detective

Glissez pour afficher le 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.

Tâche

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 desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
single

single

some-alt