繁体   English   中英

Python'builtin_function_or_method'对象在列表列表中没有属性'__getitem__'

[英]Python 'builtin_function_or_method' object has no attribute '__getitem__' on list of lists

通常,此错误表示括号中存在问题(例如,使用括号,丢失,错误的位置等),但此处似乎不是问题所在。

#iterate through all the tiles on the map and set as a background color
for y in range(MAP_WIDTH):
    for x in range(MAP_HEIGHT):
        #checks if the tile is a wall
        wall = map[x][y].block_sight
        if wall:
            rlib.console_set_char_background(
            con, x, y, color_dark_wall, rlib.BKGND_SET)
        else:
            rlib.console_set_char_background(
            con, x, y, color_dark_ground, rlib.BKGND_SET)

错误发生在墙=地图线。 该映射是一个列表列表,用于模拟python中数组的功能。 block_sight是true还是false并在此处设置:

class Tile:
    #Tiles are components of the map
    def __init__(self, blocked, block_sight = None):
        #takes the information and stores it on the tile
        self.blocked = blocked

        #if not specified, block_sight if the same as blocked
        if block_sight is None: block_sight = blocked
        self.block_sight = block_sight

任何帮助,将不胜感激。

编辑:这是构造地图的方式:

#generates a list of lists with empty tiles as elements
def makemap():
    map = [[Tile(False) #Must call a conctructor, not a variable such as floor
        for y in range(MAP_HEIGHT)] #uses comprehension to generate lists
            for x in range(MAP_WIDTH)]

    #place two pillars to test the map
    map[30][22].blocked = True
    map[30][22].block_sight = True
    map[50][22].blocked = True
    map[50][22].block_sight = True

在函数makemap()中,我忘记了将map用作全局变量,因此任何需要使用此函数构建的map的东西都无法访问它。 将“全局地图”添加为函数的第一行解决了该错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM