繁体   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]

当拿我的代码时,我得到一个错误

if L[num] > L[index]:

IndexError: list index out of range

我不知道为什么我的索引超出范围,例如,如果num = 2则索引= 3不超出范围,请帮助我

由于出现len(L)+1而导致错误

print(inc)之前添加print num, index, count ,您将解决它

暂无
暂无

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

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