2016-08-29 10 views
1

私はクラス関数に少し問題があります。私のクラスでは、私は3つの異なる関数を持っていますが、クラスの外にある関数の1つを呼び出すときは、正しい関数名を入力しても、最初の関数を呼び出すだけです。同じクラスで異なる関数を呼び出すと、最初の関数だけが呼び出されます(Python)

これは以下のクラスで、さまざまな機能を備えていますが、2つしか含まれていませんが、たくさんのコードを検索する必要はありません。

class mage(baseclass): 
    def __init__(self, name, level, attack, defence, hp): 
     baseclass.__init__(self, name, level, hp) 
     self.attack = attack 
     self.defence = defence 
    def __str__(self): 
      return "You are now a Mage, your new stats are:\n Level: {0}\n Attack: {1}\n Defence: {2}\n HP: {3}".format(self.level, self.attack, self.defence, self.hp) 
    def flamevortex(self, x, y, z): 
     print("You used Flame Vortex") 
     time.sleep(1.5) 
     damageofmove = 3 
     damagedone = damageofmove*y 
     damagedoneafterdefence = damagedone - z 
     x = x - damagedoneafterdefence 
     print("The monster's health is now " + str(x)) 
     time.sleep(1.5) 
     return x 
    def lightningbolt(self, x, y, z): 
     print("You used Lightning Bolt") 
     time.sleep(1.5) 
     damageofmove = 3 
     damagedone = damageofmove*y 
     damagedoneafterdefence = damagedone - z 
     x = x - damagedoneafterdefence 
     print("The monster's health is now " + str(x)) 
     time.sleep(1.5) 
     return x 

これは私が関数を呼び出していないのです場所です:

if Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX": 
       monster1.hp = p1.flamevortex(monster1.hp, p1.attack, monster1.defence) 
       if chosenmove == monsterattacks[0]: 
        p1.hp = monsterlasersword(p1.hp) 
       elif chosenmove == monsterattacks[1]: 
        p1.hp = monsterswipe(p1.hp) 
       elif chosenmove == monsterattacks[2]: 
        monster1.hp = monsterregen(monster1.hp) 
       time.sleep(1.5) 
       print("After the monster's attacks, your hp is now " + str(p1.hp)) 
      elif Userattack.upper() == "LIGHTNINGBOLT" or "LIGHTNING BOLT": 
       monster1.hp = p1.lightningbolt(monster1.hp, p1.attack, monster1.defence) 
       if chosenmove == monsterattacks[0]: 
        p1.hp = monsterlasersword(p1.hp) 
       elif chosenmove == monsterattacks[1]: 
        p1.hp = monsterswipe(p1.hp) 
       elif chosenmove == monsterattacks[2]: 
        monster1.hp = monsterregen(monster1.hp) 
       time.sleep(1.5) 
       print("After the monster's attacks, your hp is now " + str(p1.hp)) 

どんなにユーザ入力、それだけで今までに最初の関数を呼ぶもの。 これは処理するのに多くの助けに感謝します。ありがとう

答えて

0

if Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX":は、"FLAMEVORTEX"と等しいか、または文字列"FLAME VORTEX"は、Trueの値を持ちます。

空の文字列はFalseで、空でない文字列はTrueです。Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX"は常にTrueです。これはあなたが意味するものではありません。

試してみてください。Userattack.upper() == "FLAMEVORTEX" or Userattack.upper()=="FLAME VORTEX"

+1

より慣用と、読みやすい方法は(Userattack.upper場合 'だろう)[ "FLAMEVORTEX"、 "難VORTEX"]'で – IanAuld

関連する問題