迭代器

實作 iternext

class Counter:
    def __init__(self, max):
        self.max = max
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current >= self.max:
            raise StopIteration
        self.current += 1
        return self.current
💻 程式碼編輯器
📤 執行結果
等待執行...
← 上一課 完成本等級 ✓