我正在尝试构建一个可以将值附加到空列表的函数
n1 = 1
n2 = 2
fn = []
我想将n1和n2一起添加,然后将该值发送到fn。
然后我想将n1和n2重新分配给序列的最后两个值。
然后我希望能够在经过一定量的迭代后停止它。
我基本上是在不使用函数
的情况下构建一个斐波那契序列生成器#s(n) = (1.618^n-(1-1.618)^n)/(5^.5)`
示例:
fn = []
def fibb(n1,n2,f_iter):
# n1 would be the first number of the sequence
# n2 would be the second number of the sequence
# f_iter would be how many iterations it would do until finished
所以如果输入是:
def fibb(1,2,10):
return fn
fn = [1,2,3,5,8,13,21,34,55,89,144,233]
#f_iter(0:1+2=3,1:2+3=5,2:3+5=8,3:5+8=13, . . . 10:89+144=233)
答案 0 :(得分:1)
您可以使用此
def fib():
first, second = 0, 1
while 1:
yield first
first, second = second, first + second