[英]IndexError: list index is out of range
这是我的“Hunt the 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)
这是完整的代码,只是为了完成:
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!")
如果缩进不好,我很抱歉,有些行会换行,我失去了位置……但我认为错误出在前 11 行。
谢谢你们
你的缩进很好,但你需要把 (0, 20) 因为实际发生的是当你在那里追加时它没有定义每个洞穴 [int] 去什么,如果你要使用字典,这将是完全可能的,但结果是你最终得到了caves = [[]*20]
所以当你for i in cave_numbers
说for i in cave_numbers
它试图从1 开始到20,但由于python 是从零开始的,当你到达时它会引发错误20 因为caves
实际上从 0 开始,一直到 19,这仍然是 20 条数据。
for i in cave_numbers:
for j in range(3):
passage_to = choice(cave_numbers)
caves[i].append(passage_to)
这将获取 1..20 范围内的每个数字,并尝试将其用作长度为 20 的列表的索引,从而获得编号为 2、3、... 20 的洞穴。当它达到 i = 20 时,它发现有列表中没有具有该索引的元素,并且死亡。
将范围从 (0,20) 更改为 (1,21) 不会像您预期的那样更改结果列表的大小。 长度保持在 20。
但是,因为您将范围列表中的值用作索引,所以您的最终索引太高了。 没有洞穴[20],因为这需要一个包含 21 个元素的列表,而您的列表只有 20 个元素(在这两种情况下)。
也许你想要这样的东西? (已测试)
#!/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)
让我试着为你分解这个问题:
首先让我们检查前几行:
cave_numbers = range(1,21)
caves = []
for i in cave_numbers:
caves.append([])
如果我们跟踪“记忆”,它看起来像这样:
cave_numbers = iterator
list(cave_numbers) = [1,2,3,4,5,...,20]
caves = [[],[],[],[],[],....,[]]
给你带来麻烦的部分就在这里发生。 正如您所看到的, cave_numbers
从 1 到 20,并且您的caves
数组中正好有 20 个空列表,但 Python 中的数组是零索引的。 这意味着数组中的第一个元素实际上位于零位置。 这是一个简单的示例,说明这意味着什么以及在您的程序中发生了什么:
my_indices = [1,2,3,4,5]
my_list = [1,2,3,4,5]
print(f'{my_list[0]}') # print out '1'
for i in my_indices:
print(f'{my_list[i]}') # print out '234' ... and then a index out of exception happens
此处发生索引越界异常是因为您使用5
作为其边界设置为[0,4]
的数组的索引。
下面的代码片段实际上产生了这个确切的问题:
for i in cave_numbers:
for j in range(3):
passage_to = choice(cave_numbers)
caves[i].append(passage_to) # here i will be substituted for '1' to 20'
当您访问caves[20]
会抛出异常,因为该数组的边界实际上是[0, 19]
。
最简单的方法是可能总是从零开始你的范围N
。 零索引(在大多数情况下)是编程中的规范,您应该习惯它。 当需要包含索引的任何用户交互时,只需添加“1”,如下所示:
print(f'You are in cave {player_location + 1}')
本质上,用户并不关心您如何在代码中索引数组,但您不向用户显示它是零索引是正确的(因为它可能会令人困惑)。
print('some text', variable)
真的很老派 - 在 python 3 中尝试f 字符串(例如: print(f'some text {variable}')
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.