def fib2(n): #return Fibonacci series up to n
"""Return a list containing the Fibonacci series up to n."""
result = []
a, b = 0, 1
while b < n:
result.append(b) #see below
a, b = b, a+b
return result
#===========================================
f35 = fib2(35) #call it
print (f35) #write the result
好吧,这就是我目前的情况。给出了输出[1,1,2,3,5,8,13,21,34]。很好,但我需要倒过来。显示[34,21,13,8,5,3,2,1,1]。我只是不知道如何应用反向命令或使用[::-1]方法。你知道吗
如果我尝试应用上面的任何一种方法,我总是会遇到很多错误。我对这个很陌生。谢谢你抽出时间。你知道吗
试试这个
有some other ways可以反转列表。所有这些都能用。你知道吗
也可以使用list对象的
reverse
方法你可以用任何一个
或者
作为一种替代方法,您可以使用
insert
而不是append
来首先以正确的顺序构建列表?你知道吗所以不是:
执行:
它将
b
放在列表的索引0处,并将所有其他元素推到一个位置。你知道吗相关问题 更多 >
编程相关推荐