简体   繁体   English

我不知道为什么我的错误列表索引超出范围

[英]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 我不知道为什么我的索引超出范围,例如,如果num = 2则索引= 3不超出范围,请帮助我

You get an error because of: len(L)+1 由于出现len(L)+1而导致错误

add print num, index, count before print(inc) and you will solve it print(inc)之前添加print num, index, count ,您将解决它

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

相关问题 Pygame 获取列表索引超出范围时出错,但我不知道如何修复(我是新手) - Pygame Error getting list index out of range but i dont know how to fix (I'm new) Python 看起来失败了,我不知道为什么:IndexError: list index out of range - Python look is failing and I dont know why: IndexError: list index out of range 为什么会出现“ IndexError:列表索引超出范围”错误? - Why do I get a “IndexError: list index out of range” error? 为什么我收到错误:列表索引超出范围 - Why I get the error: list index out of range 为什么我收到错误“列表索引超出范围”以及如何修复它? - Why i get an error "list index out of range" and how to fix it? IndexError:列表索引超出范围,但我“知道”它是 - IndexError: list index out of range but I “know” it is 我在python中有“列表索引超出范围”,但是我不明白为什么 - i have “list index out of range” in python, but i dont understand why 列表索引超出范围但不是(我不认为) - List index out of range but it's not (i dont think) 为什么在打印列表时没有出现错误时出现 IndexError: string index out of range? - why getting IndexError: string index out of range when i get no error when i print the list? 为什么会出现索引超出范围错误? 错误如下, - Why do I get index out of range error? The error is as follows,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM