Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Create a Countdown Iterator | Understanding Iterators
Efficient Data Handling in Python

bookChallenge: Create a Countdown Iterator

Opgave

Swipe to start coding

Create a class Countdown that acts as an iterator, yielding numbers from a specified starting value down to zero. The iteration should stop after yielding zero, and any further calls should raise StopIteration.

  • The Countdown class must implement the iterator protocol by defining both __iter__ and __next__ methods.
  • The __next__ method should yield the current value, then decrement it by one each time it is called.
  • Once the value goes below zero, __next__ must raise StopIteration.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
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

close

bookChallenge: Create a Countdown Iterator

Stryg for at vise menuen

Opgave

Swipe to start coding

Create a class Countdown that acts as an iterator, yielding numbers from a specified starting value down to zero. The iteration should stop after yielding zero, and any further calls should raise StopIteration.

  • The Countdown class must implement the iterator protocol by defining both __iter__ and __next__ methods.
  • The __next__ method should yield the current value, then decrement it by one each time it is called.
  • Once the value goes below zero, __next__ must raise StopIteration.

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 1. Kapitel 3
single

single

some-alt