简体   繁体   English

Python 3 Fibonacci序列(列表)

[英]Python 3 Fibonacci Sequence (lists)

I'm trying to make a program in python that adds the fibonacci number in a list and then prints out the list, I don't know what I'm doing wrong but it keeps giving this error IndexError: list index out of range 我正在尝试在python中创建一个程序,在列表中添加斐波纳契数,然后打印出列表,我不知道我做错了但是它一直给出这个错误IndexError:list index超出范围

` `

size = 8
ls = [0, 1]
counter = 2

while size > 0:
    ls[counter].append(ls[counter - 1] + ls[counter - 2])
    size -= 1
    counter += 1
print(ls)

` `

It's just that you cannot append to ls[counter] but to ls 只是你不能附加到ls[counter]而是ls

size = 8
ls = [0, 1]
counter = 2
while size > 0:
    ls.append(ls[counter - 1] + ls[counter - 2])
    size -= 1
    counter += 1

print (ls)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM