Challenge: Create a Countdown Iterator
Taak
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.
Oplossing
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 1. Hoofdstuk 3
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 6.67
Challenge: Create a Countdown Iterator
Veeg om het menu te tonen
Taak
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.
Oplossing
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 1. Hoofdstuk 3
single