Challenge: Create a Countdown Iterator
Tarea
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.
Solución
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 1. Capítulo 3
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 6.67
Challenge: Create a Countdown Iterator
Desliza para mostrar el menú
Tarea
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.
Solución
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 1. Capítulo 3
single