Python 理解类和调用方法Py 2.7
好吧,我已经把我的额头放进了一些代码中,我已经确定我现在只是在黑暗中拍摄。出于某种原因,让类点击是一件非常痛苦的事情,我在想,如果我使用自己的个人代码,可能会有意义。所以这里有一个小脚本游戏,我写了实验。(这很粗糙,我是新来的): 我觉得要理解我做错了什么,我必须在我自己的代码中指出它。我敢肯定,有很多问题与我的问题相似,但这些问题让我很难过 我得到的错误是:Python 理解类和调用方法Py 2.7,python,class,python-2.x,Python,Class,Python 2.x,好吧,我已经把我的额头放进了一些代码中,我已经确定我现在只是在黑暗中拍摄。出于某种原因,让类点击是一件非常痛苦的事情,我在想,如果我使用自己的个人代码,可能会有意义。所以这里有一个小脚本游戏,我写了实验。(这很粗糙,我是新来的): 我觉得要理解我做错了什么,我必须在我自己的代码中指出它。我敢肯定,有很多问题与我的问题相似,但这些问题让我很难过 我得到的错误是: Attribute Error: Combat instance has no __Call__ method 这是我认为问题所在的课
Attribute Error: Combat instance has no __Call__ method
这是我认为问题所在的课程:
class Combat:
def battle(self):
print start.player, " has: ", hp, "."
print "The zombie has: ", zombie.zom_health()
time.sleep(1)
if haf == True:
total_damage = hero_attacks * hero_damage
zombie.zom_health() - total_damage
print "You strike the zombie for %d damage!" %(total_damage)
print "The zombie's health is %d" %zombie.zom_health()
return zombie.zom_health()
time.sleep(3)
elif haf == False:
total_damage = zombie.zom_damage()- hero.hero_armor()
if total_damage > 0:
total_damage - hp
return hp
print "A zombie shambles through the baricade and damages you!"
print "You lost %d hp! Your hp is now: %d" %(total_damage, hp)
combat_loop()
time.sleep(3)
elif total_damage <= 0:
print "A zombie lurches at you but misses!"
time.sleep(3)
combat_loop()
else:
z.zom_killed()
def initv(battle):
bat = battle()
hero_init = random.randint(1,20)
zom_init = random.randint(1,20)
if hero_init >= zom_init:
#global haf Ignoring for now
haf = True
print "You attack first!"
bat.battle()
elif hero_init < zom_init:
#global haf
haf = False
print "The zombie attacks!"
bat.battle()
def start(): #The only fucking code that works
global zombies
zombies = random.randint(20,30)
arm = random.sample(armor,1)
wea = random.sample(weapon, 1)
player = raw_input("What is your name?")
print player, ",\n"
print "Your colony is under attack by %s zombies!" %(zombies)
print "Hold them off so the others can escape."
print "You are wearing an ", arm, "and weilding a", wea
time.sleep(3)
def combat_loop():
combat_call = Combat()
while zombies > 0:
combat_call.initv()
if zombies <= 0:
print "You held off the zombies off long enough to escape!"
print "With the way clear you grab your belongings and follow suit."
print "The end!"
sys.exit()
职业战斗:
def战斗(自我):
打印start.player,“has:,hp.”
打印“僵尸有:”,zombie.zom_health()
时间。睡眠(1)
如果haf==真:
总伤害=英雄攻击*英雄伤害
僵尸。僵尸健康()-总伤害
打印“你攻击僵尸造成%d点伤害!”%(总伤害)
打印“僵尸的健康状况为%d”%zombie.zom\u health()
返回zombie.zom_health()
时间。睡眠(3)
elif haf==假:
总伤害=僵尸。僵尸伤害()-英雄。英雄盔甲()
如果总损坏>0:
总伤害-hp
返回hp
打印“一个僵尸摇摇摆摆地穿过酒吧间并伤害你!”
打印“你损失了%d生命!你的生命现在是:%d”%(总伤害,生命)
战斗循环()
时间。睡眠(3)
elif总伤害=僵尸初始:
#暂时忽略全球haf
haf=真
打印“你先攻击!”
bat.battle()
elif hero_init0时:
战斗呼叫.initv()
如果僵尸我假设initv
实际上是一种战斗
的方法,但是你忘了让它把self
作为一个参数,取而代之的是一个名为battle
的参数
当您调用bat.initv()
时,它将self
传递为batle
(self
是约定的名称;方法的第一个位置参数是self
,无论您决定如何调用它)。因此,当您在initv
中执行bat=battle()
时,这与执行self()
是一样的,即尝试将类的实例视为可调用的
据我所知,真正的目标是调用battle
方法,因此initv
的定义和第一行应该是:
def initv(self):
bat = self.battle()
它通过标准名称下的self
,然后在其上调用battle
方法。有点不清楚battle
方法返回的是什么(它似乎在两个代码路径上隐式返回None
,以及任何zombie.zom_health()
在第三个代码路径上返回,其中有一个sleep
,由于return
抢占它,它永远不会发生),但是这段代码有很多问题,很难确定什么是“正确的”行为
作为记录,错误几乎肯定是抱怨缺少\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
方法,而不是\uuuuuuuuuuuuuuuuuuuuuuu 我假设initv
实际上是一种battle
的方法,但是你忘了让它把self
作为一个参数,取而代之的是一个名为battle
的参数
当您调用bat.initv()
时,它将self
传递为batle
(self
是约定的名称;方法的第一个位置参数是self
,无论您决定如何调用它)。因此,当您在initv
中执行bat=battle()
时,这与执行self()
是一样的,即尝试将类的实例视为可调用的
据我所知,真正的目标是调用battle
方法,因此initv
的定义和第一行应该是:
def initv(self):
bat = self.battle()
它通过标准名称下的self
,然后在其上调用battle
方法。有点不清楚battle
方法返回的是什么(它似乎在两个代码路径上隐式返回None
,以及任何zombie.zom_health()
在第三个代码路径上返回,其中有一个sleep
,由于return
抢占它,它永远不会发生),但是这段代码有很多问题,很难确定什么是“正确的”行为
作为记录,错误几乎肯定是抱怨缺少\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
方法,而不是\uuuuuuuuuuuuuuuuuuuuuuu 在战斗循环中
将战斗调用
定义为战斗()
。现在的batch\u call
是batch
的一个实例。然后在while
循环中,你说battle\u call.initv()
。由于战斗呼叫
是战斗
的一个实例,这是战斗.initv(战斗呼叫)
的快捷方式。也就是说,battle\u call
是给initv()
的唯一参数。在initv()
中,使用一个参数:bat
>>> reload(Zombie)
>>> z1 = Zombie.Zombie(20)
>>> z2 = Zombie.Zombie(zom_health=30)
>>> z1.zom_health()
20
>>> z2.zom_health()
30
>>> z2.zom_damage(z2.zom_health())
30
>>> z1 - z2
>>> z1.zom_health()
-10
>>>