#生成器生成斐波那契数列 def Fibnacc(num): 2 a= 1 3 b = 1 4 current_index = 0 5 while current_index<num: 6 data = a 7 8 current_index+=1 9 a,b=b,a+b 10 yield data 11 # yield:1.充当返回值的作用2.保存程序的运行状态,并且暂停程序执行3.当next的时候,可以继续换行程序从yield位置继续向下 12 # 执行 13 14 if __name__ == ‘__main__‘: 15 fib = Fibnacc(6) 16 for i in range(6): 17 value = next(fib) 18 print(i,"列",value)
原文地址:https://www.cnblogs.com/liuxjie/p/11991629.html
时间: 2024-10-27 18:48:45