count()
itertools模块中的Usin函数提供了均匀间隔的值的迭代器。该函数有两个参数。start默认为0,step默认为1。使用默认值将生成无限迭代器。使用break终止循环。
import itertools percentNumbers = [ ] finish = "n" num = "0" for x in itertools.count() : num = input("enter the mark : ") num = float(num) percentNumbers.append(num) finish = input("stop? (y/n) ") if finish=='y':break print(percentNumbers)
上面脚本的示例输出
enter the mark : 11 stop? (y/n) enter the mark : 22 stop? (y/n) enter the mark : 33 stop? (y/n) y [11.0, 22.0, 33.0]