Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
index error
#1
I don't know why these codes makes error message.
Please help me


def dynamic_sort(C):
    C.sort(key = lambda x: x[1])  
    n = len(C)                   
    cache = []               
   
    for y in range(0, n):         
        cache.append(C[y][2])    
    
    return cache

def dynamic_job(C):
    cache = dynamic_sort(C)    
    n = len(C)                   
    for i in range(1, n+1):       
        for k in range(0, i):     
            if C[k][1] <= C[i][0]:         #this line makes the error message(IndexError: list index out of range)
                if cache[k] + C[i][2] > cache[i]:
                    cache[i] = cache[k] + C[i][2] 

    max = 0                      
    for i in range (0, n+1):     
        if max < cache[i]:        
            max = cache[i]       
    return max                    

    



C = [[2,5,6], [4,6,5], [6,7,4], [1,3,5], [5,8,11], [7,9,2]] 

print(dynamic_job(C))
buran write Dec-05-2020, 10:01 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
index error - by surim - Dec-05-2020, 09:51 AM
RE: index error - by buran - Dec-05-2020, 10:02 AM
RE: index error - by surim - Dec-05-2020, 10:04 AM
RE: index error - by surim - Dec-05-2020, 10:05 AM
RE: index error - by deanhystad - Dec-05-2020, 02:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyscript index error while calling input from html form pyscript_dude 2 1,142 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 890 May-15-2023, 03:28 PM
Last Post: buran
  I'm getting a String index out of range error debian77 7 2,589 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  Python Error List Index Out of Range abhi1vaishnav 3 2,503 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  Index error - columns vs non-column Vinny 3 5,124 Aug-09-2021, 04:46 PM
Last Post: snippsat
  How to resolve Index Error in my code? codify110 6 3,284 May-22-2021, 11:04 AM
Last Post: supuflounder
  I have an index error inline 76 but I write the program in a way that cant reach tha abbaszandi 2 2,189 Nov-13-2020, 07:43 AM
Last Post: buran
  when this error rise?index 28 is out of bounds for axis 0 with size 13 abbaszandi 1 5,161 Nov-10-2020, 08:46 PM
Last Post: deanhystad
  List index out of range error while accessing 2 lists in python K11 2 2,237 Sep-29-2020, 05:24 AM
Last Post: K11
  List index out of range error when attempting to make a basic shift code djwilson0495 4 3,194 Aug-16-2020, 08:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020