Python 如何根据变量使循环开始、中断并重新开始?
我正在尝试制作一个处理用户健康状况的模块 如果Python 如何根据变量使循环开始、中断并重新开始?,python,class,Python,Class,我正在尝试制作一个处理用户健康状况的模块 如果self.hearts为0,则用户死亡,并将其打印出来。 但是,如果用户没有死亡,并且self.hearts小于10,则该用户将每20秒治愈一次 有了下面的代码,monitor功能只会治愈用户,而不会打印用户已死亡(尤其是发生死亡时)。我做错了什么 import threading class Health(object): def __init__(self): self.hearts = 10 def moni
self.hearts
为0,则用户死亡,并将其打印出来。
但是,如果用户没有死亡,并且self.hearts
小于10,则该用户将每20秒治愈一次
有了下面的代码,monitor
功能只会治愈用户,而不会打印用户已死亡(尤其是发生死亡时)。我做错了什么
import threading
class Health(object):
def __init__(self):
self.hearts = 10
def monitor(self):
if self.hearts <= 10 and self.hearts > 0:
def heal():
threading.Timer(20.0, heal).start()
if self.hearts < 10 and self.hearts > 0:
self.hearts += 1
print '+1 heart! :D'
print 'hearts %d' % self.hearts
heal()
elif self.hearts < 1:
print 'You are dead'
quit()
def add(self, amount):
if self.hearts < 10:
self.hearts += amount
print '+{} hearts! :D'.format(amount)
else:
print 'You are full of hearts'
def remove(self, amount):
self.hearts -= amount
print '-{} hearts! :\'('.format(amount)
def health(self):
print 'Hearts: ', self.hearts
me = Health()
me.monitor()
您应该保存计时器对象,以便在
self.hearts
归零时对其调用cancel()
例如(在heal()
)中):
在删除()
中:
def移除(自身、金额):
self.hearts-=金额
打印'-{}颗心!:\'(’格式(金额)
如果self.hearts<1:
自愈\u计时器。取消()
打印“你死了”
您应该保存计时器对象,以便在self.hearts
归零时调用cancel()
例如(在heal()
)中):
在删除()
中:
def移除(自身、金额):
self.hearts-=金额
打印'-{}个红心!:\'('.格式(金额)
如果self.hearts<1:
自愈\u计时器。取消()
打印“你死了”
您应该保存计时器对象,以便在self.hearts
归零时调用cancel()
例如(在heal()
)中):
在删除()
中:
def移除(自身、金额):
self.hearts-=金额
打印'-{}个红心!:\'('.格式(金额)
如果self.hearts<1:
自愈\u计时器。取消()
打印“你死了”
您应该保存计时器对象,以便在self.hearts
归零时调用cancel()
例如(在heal()
)中):
在删除()
中:
def移除(自身、金额):
self.hearts-=金额
打印'-{}个红心!:\'('.格式(金额)
如果self.hearts<1:
自愈\u计时器。取消()
打印“你死了”
尝试此版本的代码
我刚注意到你不用定时器测试死亡,试试这个版本
def monitor(self):
if self.hearts <= 20 and self.hearts > 0:
def heal():
if self.hearts < 10 and self.hearts > 0:
self.hearts += 1
print '+1 heart! :D'
print 'hearts %d' % self.hearts
threading.Timer(10.0, heal).start()
elif self.hearts < 1:
quit()
heal()
def监视器(自身):
如果self.0:
def heal():
如果self.hearts<10且self.hearts>0:
self.hearts+=1
打印“+1颗心!:D”
打印“红心%d”%self.hearts
threading.Timer(10.0,heal.start())
elif self.hearts<1:
退出
愈合()
您还需要在删除时进行死亡测试,以避免等待下一次计时器检查
def remove(self, amount):
self.hearts -= amount
print '-{} hearts! :\'('.format(amount)
if self.hearts < 1:
print 'You are dead'
def移除(自身、金额):
self.hearts-=金额
打印'-{}个红心!:\'('.格式(金额)
如果self.hearts<1:
打印“你死了”
编辑以避免代码重复尝试此版本的代码 我刚注意到你不用定时器测试死亡,试试这个版本
def monitor(self):
if self.hearts <= 20 and self.hearts > 0:
def heal():
if self.hearts < 10 and self.hearts > 0:
self.hearts += 1
print '+1 heart! :D'
print 'hearts %d' % self.hearts
threading.Timer(10.0, heal).start()
elif self.hearts < 1:
quit()
heal()
def监视器(自身):
如果self.0:
def heal():
如果self.hearts<10且self.hearts>0:
self.hearts+=1
打印“+1颗心!:D”
打印“红心%d”%self.hearts
threading.Timer(10.0,heal.start())
elif self.hearts<1:
退出
愈合()
您还需要在删除时进行死亡测试,以避免等待下一次计时器检查
def remove(self, amount):
self.hearts -= amount
print '-{} hearts! :\'('.format(amount)
if self.hearts < 1:
print 'You are dead'
def移除(自身、金额):
self.hearts-=金额
打印'-{}个红心!:\'('.格式(金额)
如果self.hearts<1:
打印“你死了”
编辑以避免代码重复尝试此版本的代码 我刚注意到你不用定时器测试死亡,试试这个版本
def monitor(self):
if self.hearts <= 20 and self.hearts > 0:
def heal():
if self.hearts < 10 and self.hearts > 0:
self.hearts += 1
print '+1 heart! :D'
print 'hearts %d' % self.hearts
threading.Timer(10.0, heal).start()
elif self.hearts < 1:
quit()
heal()
def监视器(自身):
如果self.0:
def heal():
如果self.hearts<10且self.hearts>0:
self.hearts+=1
打印“+1颗心!:D”
打印“红心%d”%self.hearts
threading.Timer(10.0,heal.start())
elif self.hearts<1:
退出
愈合()
您还需要在删除时进行死亡测试,以避免等待下一次计时器检查
def remove(self, amount):
self.hearts -= amount
print '-{} hearts! :\'('.format(amount)
if self.hearts < 1:
print 'You are dead'
def移除(自身、金额):
self.hearts-=金额
打印'-{}个红心!:\'('.格式(金额)
如果self.hearts<1:
打印“你死了”
编辑以避免代码重复尝试此版本的代码 我刚注意到你不用定时器测试死亡,试试这个版本
def monitor(self):
if self.hearts <= 20 and self.hearts > 0:
def heal():
if self.hearts < 10 and self.hearts > 0:
self.hearts += 1
print '+1 heart! :D'
print 'hearts %d' % self.hearts
threading.Timer(10.0, heal).start()
elif self.hearts < 1:
quit()
heal()
def监视器(自身):
如果self.0:
def heal():
如果self.hearts<10且self.hearts>0:
self.hearts+=1
打印“+1颗心!:D”
打印“红心%d”%self.hearts
threading.Timer(10.0,heal.start())
elif self.hearts<1:
退出
愈合()
您还需要在删除时进行死亡测试,以避免等待下一次计时器检查
def remove(self, amount):
self.hearts -= amount
print '-{} hearts! :\'('.format(amount)
if self.hearts < 1:
print 'You are dead'
def移除(自身、金额):
self.hearts-=金额
打印'-{}个红心!:\'('.格式(金额)
如果self.hearts<1:
打印“你死了”
编辑以避免代码重复仍然不起作用
>>>我。删除(5)-5颗心!:'(>>+1颗心!:D颗心6个我。删除(10)-10颗心!:'(>>>
所以显然,实现我想要避免的目标的唯一方法是重复代码:/仍然不起作用>>我。删除(5)-5颗心!:'(>>>+1颗心!:D颗心6个我。移除(10)-10颗心!:“(>>>
显然实现我所想的唯一方法是重复代码,这是我想要避免的:/仍然不起作用。>>我。移除(5)-5颗心!:”(>>>+1颗心!:D颗心6个我。移除(10)-10颗心!:“(>>>
显然是实现我所想的唯一方法