Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器:列表索引超出范围_Python_List_Range_Indexoutofrangeexception - Fatal编程技术网

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

Python 索引器:列表索引超出范围,python,list,range,indexoutofrangeexception,Python,List,Range,Indexoutofrangeexception,在我跌跌撞撞地阅读教程的过程中,我遇到了一个反复出现的问题(我称之为反复出现,因为在我放弃一天之后,它似乎会自动解决,后来又开始出现,我不知道我是怎么做到的)错误: Traceback (most recent call last): File "roguelike.py", line 215, in <module> make_map() File "roguelike.py", line 131, in make_map create_room(new_r

在我跌跌撞撞地阅读教程的过程中,我遇到了一个反复出现的问题(我称之为反复出现,因为在我放弃一天之后,它似乎会自动解决,后来又开始出现,我不知道我是怎么做到的)错误:

Traceback (most recent call last):
  File "roguelike.py", line 215, in <module>
    make_map()
  File "roguelike.py", line 131, in make_map
    create_room(new_room)
  File "roguelike.py", line 84, in create_room
    map[x][y].blocked = False
IndexError: list index out of range
最后,我将一个字符一个字符地编写的代码与教程中编写的示例代码进行了比较,但运气不佳。这是第一次在程序中使用“map”,当涉及到python中的列表时,我非常困惑


最后,我以同样的精神探讨了其他问题,但由于知识有限,我无法将它们应用于我的情况。

至少有一个
room.x1+1
room.x2
room.y1+1
room.y2
超出了您的地图。检查地图大小或限制访问权限

备选案文1:

备选案文2:

def create_room(room):
    global map
    #set all tiles inside the room to passable

    # limit room coordinates. This is free of additional handlings but
    # might create odd rooms in the corner of the map.
    max_x, max_y = len(map) - 1, len(map[0]) - 1
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[min(max_x, x)][min(max_y, y)].blocked = False
            map[min(max_x, x)][min(max_y, y)].block_sight = False

老实说,我会避开任何使用变量名的教程,比如
map
(它会隐藏内置函数
map()
)。出于兴趣,教程是什么?我在这里看到了很多关于地图和房间的Python问题。如果你想要答案,我想你需要在这里添加一些上下文。什么是地图?它应该有多大?本教程的其余部分做什么?链接到教程可能也会有帮助。仅根据您所发布的内容,很难给您一个有用的答案。@MrE,这是我正在使用的教程。@BrentNash这是到我所处位置的代码链接,所讨论的代码直接在课堂作业之后。
class RoomOutsideError(Exception):
    pass

# ...

def create_room(room):
    global map
    #set all tiles inside the room to passable

    # check for limits and raise exception
    #    (this will need handling outside this function)
    max_x, max_y = len(map) - 1, len(max[0]) - 1
    if room.x1+1 > max_x or room.x2 > max_x or \
       room.y1+1 > max_y or room.y2 > max_y:
           raise RoomOutsideError("Room outside map.")

    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[x][y].blocked = False
            map[x][y].block_sight = False
def create_room(room):
    global map
    #set all tiles inside the room to passable

    # limit room coordinates. This is free of additional handlings but
    # might create odd rooms in the corner of the map.
    max_x, max_y = len(map) - 1, len(map[0]) - 1
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[min(max_x, x)][min(max_y, y)].blocked = False
            map[min(max_x, x)][min(max_y, y)].block_sight = False