2017-07-02 390 views
-1

我需要調用attack()checkLife()函數,但那些函數不返回任何值。我能做些什麼來解決這個問題?Python:函數沒有返回任何值

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) 
+1

當然如果它不返回你什麼,因爲你不返回任何 – Li357

+0

嗨侯賽因,歡迎SO。請花點時間仔細研究一下,以便其他人可以更輕鬆地幫助您:https://stackoverflow.com/help/how-to-ask – petezurich

回答

1

你忘了加括號()

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)