好的,所以我真的开始对此感到恼火。我似乎无法弄清楚它是什么?有人说这是缩进,但我已经尝试过了。
这是我的代码:
class player(object):
def __init__(self):
self.nation_name = None
self.race = None
self.name = None
self.special_unit = None
self.special_unitsize = 0
self.regular_army = 50
self.army_size = special_unitsize +regular_army
self.army_power = 250
self.IceColdCoins = 0
self.gold = 100
self.taxincome = 45
self.mineincome = 0
self.recruitrate = 30
self.totalincome = taxincome + mineincome
self.num_of_mines = 1
self.num_of_towns = 0
self.num_of_cities = 1
self.num_of_fort = 0
self.wait_till_upgrade = 0
每当我尝试添加任何类型的变量时,它都不允许它说: 未定义属性错误(示例)。
我做了一个测试,并发现了同样的错误。复制代码并添加一个变量,并对该变量进行if声明。
答案 0 :(得分:2)
您忘记使用self
:
self.taxincome = 45
self.mineincome = 0
self.totalincome = taxincome + mineincome # there is no 'taxincome', etc.
前两行不在本地(或全局)名称空间中生成taxincome
和mineincome
变量,而是self
的属性:
self.taxincome = 45
self.mineincome = 0
self.totalincome = self.taxincome + self.mineincome # note the 'self'
special_unitsize
和regular_army
也是如此。