Home > Blockchain >  Why is there an "index out of range" here?
Why is there an "index out of range" here?

Time:04-12

I'm currently working on a homework assignment, and the topic is to derive a Fibonacci sequence from a Pascal triangle. It sounds simple, but I came upon a lot of trouble when trying to use my method.

I basically attempted to add all of the diagonal numbers of a Fibonacci sequence by reproducing a Pascal triangle, saving all of the numbers (within the same row) into a list, and then adding up the diagonal numbers.

 

#pretend fibnumber exists, the Fibonacci number needed
# input n
fibnumber=144
n = 5
list=[]
m=''
sumslist=[]
for i in range(n):
    for j in range(i 1):
        d=factorial(i)//(factorial(j)*factorial(i-j))
        m =str(d)


    # for new line
    list.append(m)
    m=''
    print()
print(list)

for thing in list:
    sum=0
    position=0
    listposition=list.index(thing)
    print(listposition)
    if len(thing)>1:
        while position!=len(thing)-1:
            sum =float(thing[position])
            position =1
            if listposition==0:
                break
            print(sum)
            thing=list[listposition-1]
        sumslist.append(sum)

Ignore the fibnumber variable, that is for the second part which I know. When I tried to run my code, I received an error for "index out of range" for line 30, which is the part about "sum =...". I have attempted many changes and spent a whole day on this, but nothing seems to work. Can someone help me with this problem? Much appreciated!

CodePudding user response:

I have made some changes in your code and I explained the fixed bugs in comments.

import math

fibnumber=144
n = 10
list=[]
m=[] 
sumslist=[]

for i in range(n):
    for j in range(i 1):
        d=math.factorial(i)//(math.factorial(j)*math.factorial(i-j))
        m.append(d)


    # for new line
    list.append(m)
    m=[] 
    print()
print(list)

for thing in list:
    sum=0
    position=0
    listposition=list.index(thing)
    row = math.floor(listposition/2)   1 # The number of required iteration -> ([rows/2] - 1)
    if len(thing)>1:
        while position!=row:
            sum =thing[position] #if you store rows  as strings, for numbers with two digits and more, it only considers the first digit. e.g 6th rows: 10 -> 1 
            position =1
            listposition-=1 # go to the preveious row
            thing=list[listposition] #This line must've been changed to fix the bug you mentioned. You change the while condition in each iteration, while it should be a fixed value: ([rows/2] - 1)
        print('sum', sum)
        sumslist.append(sum)
print(sumslist)

CodePudding user response:

I don't quite follow your logic here, but im just gonna point out two obvious mistake

for thing in list:
    sum=0
    position=0
    listposition=list.index(thing)
    print(listposition)
    if len(thing)>1:
        while position!=len(thing)-1:
            sum =float(list[position])# changed from things to list because things dont have index
            position =1
            if listposition==0:# this will never break because when listposition == 0, len(thing)=1
                break
            print(sum)
            thing=list[listposition-1]
        sumslist.append(sum)
  • Related