1 Star 0 Fork 0

abel/python-small-examples

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
Descend.py 487 Bytes
Copy Edit Raw Blame History
guozhen3 authored 2020-01-16 20:56 +08:00 . src files
from collections.abc import Iterator
#编写一个迭代器,通过循环语句,实现对某个正整数的依次递减1,直到0.
class Descend(Iterator):
def __init__(self,N):
self.N=N
self.a=0
def __next__(self):
while self.a<self.N:
self.N-=1
return self.N
raise StopIteration
descend_iter=Descend(10)
try:
for i in descend_iter:
print(i)
except StopIteration as e:
print('迭代结束')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/PythonIoT/python-small-examples.git
git@gitee.com:PythonIoT/python-small-examples.git
PythonIoT
python-small-examples
python-small-examples
master

Search