Challenge: Create a Countdown Iterator
Oppgave
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
Countdownclass 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 raiseStopIteration.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 3
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 6.67
Challenge: Create a Countdown Iterator
Sveip for å vise menyen
Oppgave
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
Countdownclass 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 raiseStopIteration.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 3
single