调用list.index(a)会导致属性调用,而不是方法?

时间:2015-06-29 21:13:34

标签: python python-3.x methods python-3.5

我一直在尝试创建一个脚本来获取字典中的列表,并编辑它们以创建类似网格的东西。我试图使变量等效于列表中值的索引,但错误表明数据中没有这样的属性。我试图调用METHOD,而不是检索属性,但这是机器认为我想做的事情。

我使用的是32位版本的Python 3.5。

这是脚本。我已对相关热线添加评论。

#Map Generation Script
GRID = {
1 : [1,1,1,1,1,1,1,1,1,1],
2 : [1,1,1,1,1,1,1,1,1,1],
3 : [1,1,1,1,1,1,1,1,1,1],
4 : [1,1,1,1,1,1,1,1,1,1],
5 : [1,1,1,1,1,1,1,1,1,1],
6 : [1,1,1,1,1,1,1,1,1,1],
7 : [1,1,1,1,1,1,1,1,1,1],
8 : [1,1,1,1,1,1,1,1,1,1],
9 : [1,1,1,1,1,1,1,1,1,1],
10 : [1,1,1,1,1,1,1,1,1,1]
}

def usepen(x):
Areaedit = GRID[godpenx]
Areaedit[godpeny] = x
GRID[godpenx] = Areaedit

def makeshapeline(tile, length):
for item in GRID:               #I know this isn't indented properly
    for unit in GRID[item]:
        if unit == tile:
            locpenx = GRID.index(item) #offending line
            locpeny = GRID[item].index(tile) 
            anglea = randrange(0,1)
            angleb = randrange(0,1)
            while anglea + angleb == 0:
                anglea = randrange(0,1)
                angleb = randrange(0,1)
            for nump in length:
                locpenx += anglea
                locpeny += angleb
                if locpenx or locpeny > 9:
                    break
                GRID[locpenx[locpeny]] = tile

usepen(0)
makeshapeline(0, randrange(1,8))
for item in GRID:
    print (GRID[item])

1 个答案:

答案 0 :(得分:0)

确实locpenx = GRID.index(item)是违规行:

你需要这样的东西 -

locpenx = GRID[1].index(item) 

locpenx = GRID[2].index(item) 

..等等。