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

Oppgave

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øsning

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

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

Sveip for å vise menyen

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.

Oppgave

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øsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 4
single

single

some-alt