[英]IndexError: index out of range
I have a loop that runs 16 times because there's 16 lines in the file it reads from, I'm using a loop to, among other things, read the first two characters of the time stamp. 我有一个循环可以运行16次,因为它读取的文件中有16行,因此我正在使用一个循环来读取时间戳的前两个字符。 But I can't figure out how to keep the n variable from reaching 17. I've tried to use while conditions to limit n to 16 but to no avail.
但是我无法弄清楚如何使n变量不达到17。我试图使用while条件将n限制为16,但无济于事。 Here is my entire function, I'm trying to animate the path of hurricane Irene as detailed at http://calicoproject.org/Calico_Python_GIS :
这是我的全部功能,我正在尝试动画艾琳飓风的路径,如http://calicoproject.org/Calico_Python_GIS所述 :
def drawIrene(win):
line = Line()
n = 0
for (x,y) in dots:
time_stamp[n] = time_stamp[n][:2]
time_stamp[n+1] = time_stamp[n+1][:2]
time1 = abs(int(time_stamp[n]) - 12)
time2 = abs(int(time_stamp[n+1]) - 12)
wait_time = abs(int(time1) - int(time2))
x, y = ll2xy(x, y)
c = Circle(Point(x, y), int(eye_size[n]))
line.append(Point(x,y))
c.fill = Color(255, 255, 0, 62)
c.draw(win)
t = Polygon((x, y), (x+10, y+5), (x+int(movement_speed[n]), y-int(movement_speed[n])/2))
t.fill = Color("orange")
t.draw(win)
print(time1)
print(time2)
print()
#print (wait_time)
wait(wait_time)
if n < len(dots):
n += 1
else:
break
line.draw(win)
line.border = 2
The issue is that you're looping over two sequences of the same length, dots
and time_stamp
. 问题是您要遍历两个相同长度的
dots
, dots
和time_stamp
。 However, you're looking ahead in the time_stamp
sequence, accessing both the current item and the next item in order to calculate the wait_time
value. 但是,您正在按
time_stamp
顺序向前看,同时访问当前项和下一项以计算wait_time
值。 This is causing your IndexError
on the last pass through the loop, since an n+1
index points off the end of the list. 这是由于循环的最后一个遍历导致您的
IndexError
,因为n+1
索引指向列表的末尾。
There are a few different ways to fix this. 有几种不同的方法可以解决此问题。 You could to modify your data structures so that either
dots
has one fewer element, or so that time_stamp
has one more. 您可以修改数据结构,以使
dots
少一个元素,或者使time_stamp
数多一个。 Or you could simply loop one fewer time, as suggested in the comments. 或者,您也可以按照注释中的建议简单地循环一小段时间。
But I think the best solution will be to change the logic in your loop to calculate wait_time
differently on either the first or last cycle. 但是我认为最好的解决方案是更改循环中的逻辑,以在第一个或最后一个周期中不同地计算
wait_time
。 For instance, you might choose to have no delay on the last cycle. 例如,您可以选择在上一个周期没有延迟。
Here's some code that does just that. 这是一些执行此操作的代码。 It special cases the last pass through, where
n
is equal to len(time_stamp)-1
, and simply sets wait_time
to zero in that case. 特殊情况是最后一次通过,其中
n
等于len(time_stamp)-1
,在这种情况下,只需将wait_time
设置为零。 Note that I'm generating n
from the built-in enumerate
function in the for
loop, so it doesn't need to be initialized to zero or updated manually: 请注意,我是通过
for
循环中的内置enumerate
函数生成n
的,因此不需要将其初始化为零或手动更新:
for n, (x, y) in enumerate(dots):
if n < len(time_stamp)-1:
time_stamp[n] = time_stamp[n][:2]
time_stamp[n+1] = time_stamp[n+1][:2]
time1 = abs(int(time_stamp[n]) - 12)
time2 = abs(int(time_stamp[n+1]) - 12)
wait_time = abs(int(time1) - int(time2))
else: # special case n >= len(time_stamp)-1, which should be the last pass
wait_time = 0
x, y = ll2xy(x, y)
c = Circle(Point(x, y), int(eye_size[n]))
line.append(Point(x,y))
c.fill = Color(255, 255, 0, 62)
c.draw(win)
t = Polygon((x, y), (x+10, y+5),
(x+int(movement_speed[n]), y-int(movement_speed[n])/2))
t.fill = Color("orange")
t.draw(win)
wait(wait_time)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.