IndexError:列表索引超出范围

时间:2022-11-20 16:41:51

Here is my code for part of my "Hunt the Wumpus (WAMPA)" game. It works if I make the range (0,20), but if I do (1,21) so there are still 20 caves, but no "Cave number 0"...which is just me being finnicky...and it tells me that the list index is out of range...not sure why moving everything up one digit in my range would do this, as there is no direct reference to a specific number in that range anywhere else...so removing 0 shouldn't be an issue, but alas...it is...here is the code where the error is from:

这是我的“寻找Wumpus(WAMPA)”游戏的部分代码。如果我制作范围(0,20),它可以工作,但如果我这样做(1,21),那么仍然有20个洞穴,但没有“洞穴编号0”......这只是我的屁股...而且它告诉我列表索引超出范围...不确定为什么在我的范围内将所有内容移动一位数就会这样做,因为在其他任何地方都没有直接引用该范围内的特定数字...所以删除0应该没有不是问题,但是唉......它是......这里是错误来自的代码:

from random import choice

cave_numbers = range(1,21)
caves = []
for i in cave_numbers:
    caves.append([])

for i in cave_numbers:
    for j in range(3):
        passage_to = choice(cave_numbers)
        caves[i].append(passage_to)

And here is the code in its entirety, just for completion's sake:

这里是完整的代码,只是为了完成:

from random import choice

cave_numbers = range(1,21)
caves = []
for i in cave_numbers:
    caves.append([])

for i in cave_numbers:
    for j in range(3):
        passage_to = choice(cave_numbers)
        caves[i].append(passage_to)


wampa_location = choice(cave_numbers)
wampa_friend_location = choice(cave_numbers)
player_location = choice(cave_numbers)
while (player_location == wampa_location or player_location == wampa_friend_location):
    player_location = choice(cave_numbers)

print("Welcome to Hunt the Wampa!")
print("You can see", len(cave_numbers), "caves.")
print("To play, just type the number")
print("of the save you wish to enter next.")

player_location != wampa_location

while True:
    if player_location != wampa_location:
        print("You are in cave", player_location)
        print("From here, you can see caves:", caves[player_location])
        if(player_location == wampa_location -1 or player_location == wampa_location +1):
              print("I smell a Wampa!")
        if(player_location == wampa_friend_location -1 or player_location == wampa_friend_location +1):
        print("I smell a stinky Wampa!")
    print("Which cave next?")
    player_input = input(">")
    if(not player_input.isdigit() or
             int(player_input) not in caves[player_location]):
          print(player_input, "isn't a cave you can see from here!")
    else:
          player_location = int(player_input)
          if player_location == wampa_location:
              print("Aargh! You got eaten by a Wampa!")
          if player_location == wampa_friend_location:
              print("Aargh! You got eaten by the Wampa's friend!")

I apologize if the indenting is bad on that, some of the lines wrap and I lost my place...but I think the error is in those first 11 lines anyway.

我很抱歉,如果缩进是不好的,有些线条包裹,我失去了我的位置......但我认为错误是在前11行中。

Thanks guys

多谢你们

4 个解决方案

#1


1  

Your indentation is fine, but you need to put (0, 20) because what actually happens is when you append there it doesn't define what each caves[int] goes to, if you were to use a dictionary this would be fully possible, but what happens is you end up with caves = [[]*20] So when you say for i in cave_numbers it tries to start at 1 and go to 20, but since python is zero-based it raises an error when you reach 20 because caves actually starts at 0 and goes to 19 which is still 20 pieces of data.

你的缩进很好,但你需要放(0,20),因为实际发生的是当你在那里附加它并没有定义每个洞穴[int]去的东西,如果你要使用字典,这将是完全可能的,但是你最终得到的是洞穴= [[] * 20]所以当你在cave_numbers中为i说它时会尝试从1开始并转到20,但是因为python是从零开始的,所以当你到达时它会引发一个错误因为洞穴实际上从0开始并且变为19,这仍然是20个数据。

#2


0  

for i in cave_numbers:
    for j in range(3):
        passage_to = choice(cave_numbers)
        caves[i].append(passage_to)

This takes each number in the range 1..20 and tries to use it as an index into a list of length 20, getting caves number 2, 3, ... 20. When it gets to i = 20, it finds that there is no element in the list with that index, and dies.

这将使每个数字在1..20的范围内,并尝试将其用作长度为20的列表的索引,获得洞穴编号2,3,... 20.当它到达i = 20时,它会发现那里在该列表中没有该索引的元素,并且死亡。

#3


0  

Changing the range from (0,20) to (1,21) does not change the size of the resulting list as you would expect. The length stays at 20.

将范围从(0,20)更改为(1,21)不会像您期望的那样更改结果列表的大小。长度保持在20。

However, because you are using the values in your range list as indexes, your final index is too high. There is no caves[20] because that would need a list of 21 elements, and your list has only 20 elements (in both cases).

但是,因为您使用范围列表中的值作为索引,所以最终索引太高。没有洞穴[20],因为那需要一个包含21个元素的列表,而你的列表只有20个元素(在这两种情况下)。

#4


0  

Maybe you want something like this? (tested)

也许你想要这样的东西? (测试)

#!/usr/local/cpython-3.3/bin/python

from random import choice

cave_numbers = range(1,21)
caves = []
for i in cave_numbers:
    caves.append([])

for index, cave in enumerate(caves):
    for j in range(3):
        passage_to = choice(cave_numbers)
        cave.append(passage_to)

#1


1  

Your indentation is fine, but you need to put (0, 20) because what actually happens is when you append there it doesn't define what each caves[int] goes to, if you were to use a dictionary this would be fully possible, but what happens is you end up with caves = [[]*20] So when you say for i in cave_numbers it tries to start at 1 and go to 20, but since python is zero-based it raises an error when you reach 20 because caves actually starts at 0 and goes to 19 which is still 20 pieces of data.

你的缩进很好,但你需要放(0,20),因为实际发生的是当你在那里附加它并没有定义每个洞穴[int]去的东西,如果你要使用字典,这将是完全可能的,但是你最终得到的是洞穴= [[] * 20]所以当你在cave_numbers中为i说它时会尝试从1开始并转到20,但是因为python是从零开始的,所以当你到达时它会引发一个错误因为洞穴实际上从0开始并且变为19,这仍然是20个数据。

#2


0  

for i in cave_numbers:
    for j in range(3):
        passage_to = choice(cave_numbers)
        caves[i].append(passage_to)

This takes each number in the range 1..20 and tries to use it as an index into a list of length 20, getting caves number 2, 3, ... 20. When it gets to i = 20, it finds that there is no element in the list with that index, and dies.

这将使每个数字在1..20的范围内,并尝试将其用作长度为20的列表的索引,获得洞穴编号2,3,... 20.当它到达i = 20时,它会发现那里在该列表中没有该索引的元素,并且死亡。

#3


0  

Changing the range from (0,20) to (1,21) does not change the size of the resulting list as you would expect. The length stays at 20.

将范围从(0,20)更改为(1,21)不会像您期望的那样更改结果列表的大小。长度保持在20。

However, because you are using the values in your range list as indexes, your final index is too high. There is no caves[20] because that would need a list of 21 elements, and your list has only 20 elements (in both cases).

但是,因为您使用范围列表中的值作为索引,所以最终索引太高。没有洞穴[20],因为那需要一个包含21个元素的列表,而你的列表只有20个元素(在这两种情况下)。

#4


0  

Maybe you want something like this? (tested)

也许你想要这样的东西? (测试)

#!/usr/local/cpython-3.3/bin/python

from random import choice

cave_numbers = range(1,21)
caves = []
for i in cave_numbers:
    caves.append([])

for index, cave in enumerate(caves):
    for j in range(3):
        passage_to = choice(cave_numbers)
        cave.append(passage_to)