“列表索引超出范围” Python 中的错误

发布于 2024-12-09 13:18:36 字数 668 浏览 0 评论 0原文

我又做了一个简单的跳棋。而且,为了跟踪跳棋要移动到的所有位置,我创建了一个列表来添加所有位置。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

生来就爱笑 2024-12-16 13:18:36

你有一个非常基本的理解问题:

>>> 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)]
>>> len(Locations)
31
>>> Locations[0] # first element
(10, 10)
>>> Locations[30] # last element
(430, 190)
>>> Locations[31] # doesn't exist!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Locations 中的元素从 0 到 30,因为总共有 31 个元素。

You have a very basic understanding problem:

>>> 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)]
>>> len(Locations)
31
>>> Locations[0] # first element
(10, 10)
>>> Locations[30] # last element
(430, 190)
>>> Locations[31] # doesn't exist!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

The elements in Locations go from 0 to 30, because there are 31 elements in total.

踏月而来 2024-12-16 13:18:36

列表索引从 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

述情 2024-12-16 13:18:36

该列表中的最大索引是 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]

怂人 2024-12-16 13:18:36

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...

梦屿孤独相伴 2024-12-16 13:18:36

Locations[31] 尝试访问列表中的第 32 项(该项不存在)

Locations[31] tries to access the 32nd item in the list (which doesn't exist)

清风不识月 2024-12-16 13:18:36

大多数编程语言的数组索引从 0 开始,而不是从 1 开始。

Indexes of arrays for most programming languages start at 0 and not 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文