# 设计一个类迭代器,做n次输出,每次输出一个随机整形
import random
class RandomIter:
def __init__(self, start, end, times):
self.start = start # 输出结果的最小值
self.end = end # 输出结果的最大值
self.max_times = times # 输出整形的次数
self.count = 0
def __iter__(self):
return self
def __next__(self):
self.count += 1
if self.count <= self.max_times:
return random.randint(self.start, self.end)
else:
raise StopIteration()
r = RandomIter(1, 50, 5)
for i in r:
print(i)
转载于:https://www.cnblogs.com/chenliang0309/p/10060392.html