2016-03-03 87 views
0

我創建了一個從文件讀取數據以獲取其實例變量的敵人類。 該文件被寫入到不同類型的敵人不同的陣列。他們工作正常。當我把它們寫到對象,然後嘗試打印他們,我得到的錯誤:Python:AttributeError:'NoneType'對象沒有屬性'type'

AttributeError: 'NoneType' object has no attribute 'type' 

如果我不打印「類型」屬性,那麼它說:

AttributeError: 'NoneType' object has no attribute 'health' 

類是:

def Enemy(object): 
    def __init__(self, TypeStats): 
     self.type = TypeStats[0][0] 
     self.health = int(TypeStats[0][1]) 
     self.strength = int(TypeStats[0][2]) 
     self.dead = False 

寫入陣列到對象,並打印該變量的代碼是:

Bandit = Enemy(BanditStats) 
print Bandit.type, Bandit.health, Bandit.strength 

我寫它作爲一個二維數組的原因是因爲當我寫從文件到陣列的數據它創建一個數組:

BanditStats = [['Bandit', '80','7']] 

我不知道爲什麼發生這種情況是很容易解決?

回答

2

您沒有使用def定義類。它應該是class

class Enemy: 
... 
+0

非常感謝你,我剛剛和另一個班級做了第二次。 –

+0

當我上它時說我不得不等待幾分鐘才能接受它。 –