Home > Mobile >  my ""list index out of range"" problem in PYTHON
my ""list index out of range"" problem in PYTHON

Time:11-30

total_task=float(input("Enter the assigned total task length(in half-hour(s)):"))
total_len=total_task*2
leng=int(total_len)
payments=[]
hours=[]
for i in range(leng):
    print("Enter the payment value( in TL) for task portion ID ",(i 1)," having length ",((i 1)*0.5)," hour(s):")
    portionLen=int(input())
    payments.append(portionLen)
    hours.append(portionLen)
paymentsTable=[]
for i in range(leng):
    paymentsRow=[]
    for j in range(leng):
        paymentsRow.append(0)
    paymentsTable.append(paymentsRow)
for i in range(leng):
    paymentsTable[i][i]=payments[i]
for i in range(leng):
    for j in range(1,leng 1):
        maxPayment=0
        for k in range(j):
            pay=paymentsTable[i][k] paymentsTable[k 1][j]
            if(pay>maxPayment):
                maxPayment=pay
        paymentsTable[i][j]=maxPayment
idTable=[]
for i in range(leng):
    idTableRow=[]
    for j in range(leng):
        idTableRow.append(0)
    idTable.append(idTableRow)
for i in range(leng):
    idTable[i][i]=i 1
for i in range(leng):
    for j in range(1,leng 1):
        maxPayment=0
        for k in range(j):
            pay = paymentsTable[i][k]   paymentsTable[k   1][j]
            if (pay > maxPayment):
                maxPayment = pay
        paymentsTable[i][j] = maxPayment
for i in range(leng):
    for j in range(1,leng 1):
        maxPayment=0
        for k in range(j):
            pay = paymentsTable[i][k]   paymentsTable[k   1][j]
            if (pay > maxPayment):
                maxPayment = pay
                idTable[i][j]=k 1

My Sample input

Enter the assigned total task length(in half-hour(s)):**2**
Enter the payment value( in TL) for task portion ID  1  having length  0.5  hour(s):
**100**
Enter the payment value( in TL) for task portion ID  2  having length  1.0  hour(s):
**400**
Enter the payment value( in TL) for task portion ID  3  having length  1.5  hour(s):
**500**
Enter the payment value( in TL) for task portion ID  4  having length  2.0  hour(s):
**600**

and my sample error

line 23, in <module>
    pay=paymentsTable[i][k] paymentsTable[k 1][j]
IndexError: list index out of range

CodePudding user response:

The error message you attached is pretty clear:

in line 23, either paymentsTable[i][k] or paymentsTable[k 1][j] has an index out of range.

paymentsTable has exactly leng elements, so their valid indices go from 0 to leng-1.

Every element paymentsTable[i] is also a list with exactly leng elements, so their valid indices also go from 0 to leng-1.

Now, i ranges inside range(leng), but j is ranging over (range(1, leng 1)), which means the first value will be 1 and the last will be leng.

Hence, in the last iteration of for j in range(1,leng 1), j is leng and the last valid index of paymentsTable[i] was leng-1, so you get the "IndexError: list index out of range"

Moreover, k 1 also gets the value leng in the last iteration of that line, it is also out of range, considering that the last valid index of paymentsTable is also leng.

CodePudding user response:

I haven't tried this yet but I think the problem is with the row assignment. paymentsRow and idTableRow must be created outside the for a loop. Your implementation means it loops through and appends, but after appending, it makes the list empty again. So basically in your code, paymentsRow and idTableRow have only one element in them.

[UPDATE] I integrated Rodrigo's solution with mine and no errors appeared.

total_task=float(input("Enter the assigned total task length(in half-hour(s)):"))
total_len=total_task*2
leng=int(total_len)
payments=[]
hours=[]
for i in range(leng):
    print("Enter the payment value( in TL) for task portion ID ",(i 1)," having length ",((i 1)*0.5)," hour(s):")
    portionLen=int(input())
    payments.append(portionLen)
    hours.append(portionLen)
paymentsTable=[]
paymentsRow=[]
for i in range(leng):
    for j in range(leng):
        paymentsRow.append(0)
    paymentsTable.append(paymentsRow)
for i in range(leng):
    paymentsTable[i][i]=payments[i]
for i in range(leng):
    for j in range(1,leng):
        maxPayment=0
        for k in range(j):
            pay=paymentsTable[i][k] paymentsTable[k 1][j]
            if(pay>maxPayment):
                maxPayment=pay
        paymentsTable[i][j]=maxPayment
idTable=[]
idTableRow=[]

for i in range(leng):
    for j in range(leng):
        idTableRow.append(0)
    idTable.append(idTableRow)
for i in range(leng):
    idTable[i][i]=i 1
for i in range(leng):
    for j in range(1,leng):
        maxPayment=0
        for k in range(j):
            pay = paymentsTable[i][k]   paymentsTable[k   1][j]
            if (pay > maxPayment):
                maxPayment = pay
        paymentsTable[i][j] = maxPayment
for i in range(leng):
    for j in range(1,leng):
        maxPayment=0
        for k in range(j):
            pay = paymentsTable[i][k]   paymentsTable[k   1][j]
            if (pay > maxPayment):
                maxPayment = pay
                idTable[i][j]=k 1
  • Related