Python 索引器错误:列表索引超出范围

Python 索引器错误:列表索引超出范围,python,list,indexing,Python,List,Indexing,这是我的“追捕Wumpus(万帕)”游戏的部分代码。如果我把范围定为(0,20),它是有效的,但如果我定为(1,21),那么仍然有20个洞穴,但没有“洞穴编号0”…这只是我的芬尼基…它告诉我列表索引超出范围…不确定为什么将我范围内的所有东西向上移动一位会这样做,由于该范围内的任何其他地方都没有对特定数字的直接引用…因此删除0不应该是一个问题,但遗憾的是…它是…以下是错误产生的代码: from random import choice cave_numbers = range(1,21) cav

这是我的“追捕Wumpus(万帕)”游戏的部分代码。如果我把范围定为(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]指向什么,如果你们使用字典,这是完全可能的,但最终你们得到的是
洞穴=[[]*20]
所以当你说
洞穴中的i时,它试图从1开始,然后转到20,但是由于python是基于零的,所以当你到达20时,它会产生一个错误,因为洞穴中的
实际上从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个元素(在这两种情况下)。

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


让我试着为您分析一下这个问题:

解释 首先,让我们检查前几行:

cave_编号=范围(1,21)
洞穴=[]
对于cave_编号中的i:
1.append([])
如果我们跟踪“内存”,它将如下所示:

cave_numbers = iterator
list(cave_numbers) = [1,2,3,4,5,...,20]
caves = [[],[],[],[],[],....,[]]
给你带来麻烦的部分就发生在这里。正如您所看到的,
cave\u编号从1变为20,您的
cave
数组中正好有20个空列表,但python中的数组是零索引的。这意味着数组中的第一个元素实际上位于零位置。下面是一个快速示例,说明这意味着什么以及在您的程序中发生了什么:

my_索引=[1,2,3,4,5]
我的清单=[1,2,3,4,5]
打印(f'{my_list[0]}')#打印出“1”
对于我的指数中的i:
打印(f'{my_list[i]}')#打印出'234'。。。然后发生了一个异常索引
这里发生索引越界异常是因为您使用
5
作为绑定设置为
[0,4]
的数组的索引

下面的代码段实际上产生了这个问题:

对于i-in-cave_编号:
对于范围(3)内的j:
通道至=选择(洞穴编号)
洞穴[i].附加(段落_to)#这里我将被替换为'1'到20'
当您访问
caves[20]
时,会引发异常,因为此数组的边界实际上是
[0,19]

如何修复它? 最简单的方法可能是始终从零开始到
N
。零索引(在大多数情况下)是编程中的标准,您应该习惯它。当需要任何包含索引的用户交互时,只需添加“1”,如下所示:

print(如果‘你在洞穴{player_location+1}’)
本质上,用户并不关心您如何在代码中为数组编制索引,但您没有向用户显示它是零索引的(因为这可能会让人困惑)

提示:
print('sometext',variable)
真是老生常谈-在python 3中试用(例如:
print(f'sometext{variable}')

cave_numbers = iterator
list(cave_numbers) = [1,2,3,4,5,...,20]
caves = [[],[],[],[],[],....,[]]