Python 2.7 Python';内置函数或方法';对象没有属性'__获取项目';关于名单
通常,此错误意味着括号存在问题(例如使用括号、缺少、错误点等),但这似乎不是问题所在Python 2.7 Python';内置函数或方法';对象没有属性'__获取项目';关于名单,python-2.7,Python 2.7,通常,此错误意味着括号存在问题(例如使用括号、缺少、错误点等),但这似乎不是问题所在 #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
#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_视线为真或假,在此处设置:
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的任何东西都无法访问该函数构建。将“全局映射”添加为函数的第一行解决了该错误。从错误中可以看出,您的列表实际上并不是您所期望的。也许外部列表包含内置函数,而不是内部列表。你能展示一下你如何创建
map
?你能让它在你调用那行之前打印出来map[x][y]
,看看你得到了什么吗?或者map
是你在函数式编程中使用的内置函数,而不是你的列表,它有其他名字?