【发布时间】:2020-09-30 14:59:01
【问题描述】:
我想创建一个函数来构造 Lucas-(或 Fibonacci-)类序列。我希望能够选择两个第一个数字,然后使下一个数字成为前两个数字的总和。我当时思考这个问题的方式是:
def lucaslike(a, b, n):
x = [a, b]
for i in range(2, n+1):
return x.append(x[i-1] + x[i-2])
但是当尝试使用它print(lucaslike(3, 6, 10)) 时,我发现该函数没有定义。
我希望能够告诉函数生成 n=10 项的类似卢卡斯的序列,例如,当两个第一个值是 a=3 和 b=6 时。所以输出应该是:[3, 6, 9, 15, 24, 39, ...]。
【问题讨论】:
-
I get that the function is not defined- 无法复制。