Python 3.x Python:函数没有´;不返回任何值
我需要调用Python 3.x Python:函数没有´;不返回任何值,python-3.x,Python 3.x,我需要调用attack()或checkLife()函数,但这些函数不返回任何值。我能做些什么来解决这个问题 class Enemy: life = 100 def choose(self,ch): return {1:self.attack, 2:self.checkLife }[ch] def attack(self): print('ouch!') self
attack()
或checkLife()
函数,但这些函数不返回任何值。我能做些什么来解决这个问题
class Enemy: life = 100
def choose(self,ch):
return {1:self.attack,
2:self.checkLife
}[ch]
def attack(self):
print('ouch!')
self.life-=1
def checkLife(self):
if(self.life <= 0):
print('I am dead')
else:
print(str(self.life) + "Life Left...")
enemy1 = Enemy()
enemy1.choose(1)
职业敌人:生命=100
def选择(自我,ch):
返回{1:自我攻击,
2:自我检查生活
}[ch]
def攻击(自我):
打印(“哎哟!”)
自我生活-=1
def检查寿命(自我):
如果(self.life您忘记添加括号()
class敌人:
寿命=100
def选择(自我,ch):
返回{1:self.attack(),#此处
2:self.checkLife()#这里
}[ch]
def攻击(自我):
打印(“哎哟!”)
自我生活-=1
def检查寿命(自我):
如果(self.life if course)它不会回报你任何东西,因为你不会回报任何东西Hi Hussein,欢迎来到SO。请花点时间研究一下,以便其他人可以更轻松地帮助你:
class Enemy:
life = 100
def choose(self,ch):
return {1:self.attack(), #here
2:self.checkLife() #and here
}[ch]
def attack(self):
print('ouch!')
self.life-=1
def checkLife(self):
if(self.life <= 0):
print('I am dead')
else:
print(str(self.life) + "Life Left...")
enemy1 = Enemy()
enemy1.choose(1)