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

bookChallenge: Create a Countdown Iterator

Uppgift

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 allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

bookChallenge: Create a Countdown Iterator

Svep för att visa menyn

Uppgift

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 desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3
single

single

some-alt