我試圖讓這個代碼工作,並與此代碼,我得到這個錯誤:添加新的座標不是爲我工作列表
File "E:\Snacks.py", line 93, in runGame
obsCoords.insert(0, newobs)
AttributeError: 'dict' object has no attribute 'insert'
那是因爲我的後置位置清單:
obsCoords = []
obscoords = getRandomLocation()
if (event.key == K_q) and len(wormCoords) >= 4:
del wormCoords[-1]
obsCoords['x'] = wormBody['x']
obsCoords['y'] = wormBody['y']
newobs = {'x': obsCoords['x'], 'y': obsCoords['y']}
obsCoords.insert(0, newobs)
if wormCoords[HEAD]['x'] == obsCoords['x'] and wormCoords[HEAD]['y'] == obsCoords['y']:
return
,所以我將其刪除:
obsCoords = []
if (event.key == K_q) and len(wormCoords) >= 4:
del wormCoords[-1]
obsCoords['x'] = wormBody['x']
obsCoords['y'] = wormBody['y']
newobs = {'x': obsCoords['x'], 'y': obsCoords['y']}
obsCoords.insert(0, newobs)
if wormCoords[HEAD]['x'] == obsCoords['x'] and wormCoords[HEAD]['y'] == obsCoords['y']:
return
以及與此錯誤結束:
TypeError: list indices must be integers, not str
我想要做的就是將新位置添加到列表中。任何人都可以幫助我嗎?
使用字典而不是列表,請參閱https://docs.python.org/2/tutorial/datastructures.html – jgrgurica
您的代碼不清楚。你想做什麼?此外,您的代碼似乎與生成錯誤的代碼不同。 –