体内有一个循环,应该调用一个函数,但这个函数的生命周期应该在循环步骤内......所以代码:
class Viking:
def __init__ (self):
self.damage = 10
self.hp = 100
def crit (self):
self.damage *= 3
x = Viking()
while True:
x.hp -= 10
if x.hp < 30:
x.crit()
在这里,在这种情况下,x.damage 将始终等于 30,但是如何使它在循环的下一步中 x.damage 再次等于 10,尽管 x.hp -= 10 将继续执行。
举个例子:
安慰: