Class 实例没有属性\uuu getitem__
我正在做一个基于web2py生活游戏的生活模拟游戏。我有两个对象,其中包含网格上每个空间中生物体的各种属性的多个数组。我收到此错误,LifeSpace实例在包含以下代码的行上没有属性“getitem”,代码为nxt[I][j]=ProcessNeightrsEmpty(I,j,cur) 我引用对象实例是否不正确Class 实例没有属性\uuu getitem__,class,web2py,Class,Web2py,我正在做一个基于web2py生活游戏的生活模拟游戏。我有两个对象,其中包含网格上每个空间中生物体的各种属性的多个数组。我收到此错误,LifeSpace实例在包含以下代码的行上没有属性“getitem”,代码为nxt[I][j]=ProcessNeightrsEmpty(I,j,cur) 我引用对象实例是否不正确 ROWS = 60 COLS = 60 GENERATIONS = 100 DELAY = 0.5 #-----------------------------------------
ROWS = 60
COLS = 60
GENERATIONS = 100
DELAY = 0.5
#---------------------------------------------------------------------------
class LifeSpace :
def __init__(self):
self.genus = []
self.species = []
self.tribe = []
self.hunger = []
self.wounds = []
self.age = []
self.genus = initGrid (COLS, ROWS, self.genus)
self.species = initGrid (COLS, ROWS, self.species)
self.tribe = initGrid (COLS, ROWS, self.tribe)
self.hunger = initGrid(COLS, ROWS, self.hunger)
self.wounds = initGrid(COLS, ROWS, self.wounds)
self.age = initGrid(COLS, ROWS, self.age)
#---------------------------------------------------------------------------
def initGrid(cols, rows, array):
for i in range(rows):
arrayRow = []
for j in range(cols):
arrayRow+= [0]
array += [arrayRow]
return array
#---------------------------------------------------------------------------
def printGen(cols, rows, array, genNo):
return("Game of Life -- Generation " + str(genNo + 1))
for i in range(rows):
for j in range(cols):
if array.species[i][j] == 0:
return(" ")
elif array.species[i][j] == 'weed':
return("3")
return("\n")
#---------------------------------------------------------------------------
def processNeighborsEmpty(x, y, array):
for j in range(y-1,y+1):
for i in range(x-1,x+1):
if not(i == x and j == y):
if array.genus[i][j] == "plant" :
if array.age[i][j] == "adult" :
array.species[x][y] = array.species[i][j]
array.genus[x][y] = array.genus[i][j]
array.age[x][y] = "seed"
return array[x][y]
#---------------------------------------------------------------------------
def processNeighborsPlant(x, y, array):
if array.age[x][y] != "adult" :
if array.species[x][y] == "tree" :
if array.age[x][y] == "sapling" :
array.age[x][y] = "adult"
elif array.age[x][y] == "seed" :
array.age[x][y] = "sapling"
elif array.age[x][y] == "seed" :
array.age[x][y] = "adult"
#---------------------------------------------------------------------------
def processNextGen(cols, rows, cur, nxt):
for i in range(0,rows):
for j in range(0,cols):
if cur.genus[i][j] == 0 :
nxt[i][j] = processNeighborsEmpty (i, j, cur)
elif cur.genus[i][j] == "plant" :
nxt[i][j] = processNeighborsPlant(i, j, cur)
#---------------------------------------------------------------------------
############################################################################
#---------------------------------------------------------------------------
def index():
thisGen = LifeSpace()
nextGen = LifeSpace()
thisGen.genus[25][25] = "plant"
thisGen.species[25][25] = "weed"
thisGen.age[25][25] = "adult"
for gens in range(GENERATIONS):
printGen(COLS, ROWS, thisGen, gens)
processNextGen(COLS, ROWS, thisGen, nextGen)
time.sleep(DELAY)
thisGen, nextGen = nextGen, thisGen
input("Finished. Press <return> to quit.")
ROWS=60
COLS=60
世代=100
延迟=0.5
#---------------------------------------------------------------------------
班级生活空间:
定义初始化(自):
self.genus=[]
self.species=[]
self.tribe=[]
self.hunger=[]
self.raws=[]
self.age=[]
self.genus=initGrid(列、行、self.genus)
self.species=initGrid(列、行、self.species)
self.tribe=initGrid(列、行、self.tribe)
self.hunger=initGrid(列、行、self.hunger)
self.hards=initGrid(列、行、self.hards)
self.age=initGrid(列、行、self.age)
#---------------------------------------------------------------------------
def initGrid(列、行、数组):
对于范围内的i(行):
arrayRow=[]
对于范围内的j(cols):
arrayRow+=[0]
数组+=[arrayRow]
返回数组
#---------------------------------------------------------------------------
def printGen(列、行、数组、GENO):
返回(“生命游戏——世代”+str(genNo+1))
对于范围内的i(行):
对于范围内的j(cols):
如果array.species[i][j]==0:
返回(“”)
elif数组.物种[i][j]=='杂草':
回报(“3”)
返回(“\n”)
#---------------------------------------------------------------------------
def processnextorsempty(x,y,数组):
对于范围内的j(y-1,y+1):
对于范围内的i(x-1,x+1):
如果不是(i==x和j==y):
如果array.genus[i][j]=“plant”:
如果array.age[i][j]=“成人”:
array.species[x][y]=array.species[i][j]
数组.属[x][y]=数组.属[i][j]
array.age[x][y]=“种子”
返回数组[x][y]
#---------------------------------------------------------------------------
def处理设备(x、y、阵列):
if array.age[x][y]!=“成人”:
如果array.species[x][y]=“tree”:
如果array.age[x][y]=“树苗”:
array.age[x][y]=“成人”
elif数组.age[x][y]=“种子”:
array.age[x][y]=“树苗”
elif数组.age[x][y]=“种子”:
array.age[x][y]=“成人”
#---------------------------------------------------------------------------
def PROCESS NEXTGEN(列、行、cur、nxt):
对于范围(0,行)中的i:
对于范围内的j(0,cols):
如果当前属[i][j]==0:
nxt[i][j]=进程邻居空(i,j,cur)
elif cur.属[i][j]=“植物”:
nxt[i][j]=加工厂(i,j,cur)
#---------------------------------------------------------------------------
############################################################################
#---------------------------------------------------------------------------
def index():
thisGen=生命空间()
nextGen=LifeSpace()
本属[25][25]=“植物”
该属物种[25][25]=“杂草”
此世代年龄[25][25]=“成人”
对于范围内的发电机组(世代):
printGen(列、行、thisGen、gens)
processNextGen(列、行、thisGen、nextGen)
时间。睡眠(延迟)
thisGen,nextGen=nextGen,thisGen
输入(“完成。按退出”)
nxt
是一个生命空间对象,因此不能使用[]
符号来引用它
您需要使用具有正确属性的
nxt.attribute[x][y]
。也许nxt.genux[x][y]
是您想要使用的。我想我需要创建一个对象数组,而不是包含数组的对象。非常感谢。