要编写接受参数n的make_fibonacci,该参数生成并返回包含fibonacci序列的第一个n+1项的元组,其中n>= 0。从其他问题上看,
def make_fibonacci(n):
a, b = 0, 1
for i in range(d):
a, b = b, a+b但是既然我需要一个元组的fibonacci的范围,就像
make_fibonacci(10)
>>> (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55)非常感谢!
发布于 2014-04-20 14:23:31
您可以将其附加到列表中:
def make_fibonacci(n):
fib = []
a, b = 0, 1
for i in range(n):
a, b = b, a+b
fib.append(a)
return tuple(fib)
make_fibonacci(10)发布于 2014-04-20 14:25:34
下面是一个简单的解决方案,构造为一个列表并返回一个元组
def make_fibonacci(n):
a = [0, 1]
[a.append(a[-1]+a[-2]) for i in xrange(n)]
return tuple(a)发布于 2014-04-20 14:23:19
def fib(n):
tup=[]
a,b = 0,1
while b<n:
tup=tup+[b,]
a,b = b,a+b
print tup您需要附加到元组,然后如果您愿意的话打印它。
https://stackoverflow.com/questions/23183473
复制相似问题