简体   繁体   中英

i dont know why i get error list index out of range

def longest_run(L):
   inc = []
   num = 0    
   count = 0    
   index = 1    
   while count < len(L)+1:

       if L[num] >= L[index]:
           inc.append(L[index])
       num +=1
       index +=1        
       count += 1

   print (inc)
L = [10, 4, 3, 8, 3, 4, 5, 7, 7, 2]

when take my code I got a error

if L[num] > L[index]:

IndexError: list index out of range

I dont know why my index would be out of range for example if num = 2 therefor index = 3 not out of range Please help me

You get an error because of: len(L)+1

add print num, index, count before print(inc) and you will solve it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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