Challenge: Create a Countdown Iterator
Compito
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.
Soluzione
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 3
single
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 6.67
Challenge: Create a Countdown Iterator
Scorri per mostrare il menu
Compito
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.
Soluzione
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 3
single