Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Power-up the Hero | Variable Scope, Nested Functions, and Closures
Functional Programming Concepts in Python
Sektion 3. Kapitel 9
single

single

Challenge: Power-up the Hero

Stryg for at vise menuen

Opgave

Swipe to start coding

Create a factory function that generates custom power-ups for a hero. Each power-up should remember its own multiplier and apply it to a hero's base stat, demonstrating closure behavior by allowing the inner function to remember the multiplier provided to the outer function.

  • Define an inner function inside create_power_up that takes a single argument base_stat and multiplies it by multiplier.
  • Return this inner function from create_power_up.
  • Use create_power_up to create two power-ups: one that doubles and one that triples a stat.
  • Apply these power-ups to a hero's attack and defense stats and print the results.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 9
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt