报错:IndexError: list assignment index out of range

>>>f = []

>>>f[0] = 0

>>>f[1] = 1

原因:空数组不能根据位置赋值

修改:用.append()方法,每一次在原数组末尾添加

>>>f = []

>>>f.append(0)

>>>f.append(1)

斐波那契数列简洁数组版:

f = []
f.append(0)
f.append(1)
for i in range(2,100):
f.append(f[i-2]+f[i-1])
print(f)
>>>[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
posted on 2020-04-09 18:01  人奇日青  阅读(253)  评论(0)    收藏  举报