“列表索引超出范围” Python 中的错误
我又做了一个简单的跳棋。而且,为了跟踪跳棋要移动到的所有位置,我创建了一个列表来添加所有位置。
Locations = [(10, 10), (70, 10), (130, 10), (190, 20), (250, 10), (310, 10), \
(370, 10), (430, 10), (10, 70), (130, 70), (190, 70), (250, 70), \
(310, 70), (370, 70), (430, 70), (10, 130), (70, 130), (130, 130), \
(190, 130), (250, 130), (310, 130), (370, 130), (430, 130), \
(10, 190), (70, 190), (130, 190), (190, 190), (250, 190), (310, 190), \
(370, 190), (430, 190)]
然而,当我尝试执行该程序时,我总是得到: Ld8 = Locations[31] - 列表索引超出范围
所以,我想也许一个列表只能包含一定数量的数字。因此,我创建了第二个位置列表并添加了 E - H 行,以便拆分列表。但是,我仍然收到索引超出范围的相同错误。 (Ld8 是存储 D 行第 8 列位置的变量)
I am making a simple Checkers again. And, in order to keep track of all the locations for the checkers to move to, I created a list to add all of the locations.
Locations = [(10, 10), (70, 10), (130, 10), (190, 20), (250, 10), (310, 10), \
(370, 10), (430, 10), (10, 70), (130, 70), (190, 70), (250, 70), \
(310, 70), (370, 70), (430, 70), (10, 130), (70, 130), (130, 130), \
(190, 130), (250, 130), (310, 130), (370, 130), (430, 130), \
(10, 190), (70, 190), (130, 190), (190, 190), (250, 190), (310, 190), \
(370, 190), (430, 190)]
However, when I try to execute the program, I always get:
Ld8 = Locations[31] - list index out of range
So, I thought maybe a list can only contain certain amount of numbers. So, I created a second Locations list and added rows E - H, so to split up the list. But, I still receive the same error of index being out of range.
(Ld8 is the variable storing the location for Row D, Column 8)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你有一个非常基本的理解问题:
Locations 中的元素从 0 到 30,因为总共有 31 个元素。
You have a very basic understanding problem:
The elements in Locations go from 0 to 30, because there are 31 elements in total.
列表索引从 0 开始。因此您的列表有 31 个项目,您可以使用索引 0-30 访问它们。 Locations[31] 指的是不存在的第 32 项
List indexes start from 0. So your list has 31 items you can access them with indexes 0-30. Locations[31] refers to the 32nd item which doesn't exist
该列表中的最大索引是 30,而不是 31。列表中的索引(在大多数语言中)从零开始。所以
(10,10)
位于索引 0 处,而(430,190)
位于索引 30 处。如果您尝试在 python 中的列表末尾建立索引,我建议您按
位置[-1]
建立索引。如果您尝试使用
len
,则需要减去1:Locations[len(Locations) - 1]
The largest index in that list is 30, not 31. Indexes in lists (in most languages) start with zero. So
(10,10)
is at index 0 and(430,190)
is at index 30.If you are trying to index at the end of the list in python, I suggest you index by
Locations[-1]
.If you are trying to use
len
, you need to subtract 1:Locations[len(Locations) - 1]
Emm...列表的元素从 0 开始编号,而不是从 1 开始。我已经数过你的列表了,我看到元素从 0 到 30。
有时我什至忘记了可以从 1 开始计数...
Emm... The list's elements are numbered from 0, not from 1. I have counted your list and I see elements from 0 to 30.
Sometimes I even forget it's possible to start counting at 1...
Locations[31] 尝试访问列表中的第 32 项(该项不存在)
Locations[31] tries to access the 32nd item in the list (which doesn't exist)
大多数编程语言的数组索引从
0
开始,而不是从1
开始。Indexes of arrays for most programming languages start at
0
and not1
.