在同一个类中调用不同的函数时,只调用第一个函数(Python)

时间:2016-08-29 16:07:14

标签: function class python-3.x

我的班级功能有点麻烦。在我的课上我有3个不同的功能,但每当我调用课外的其中一个函数时,尽管输入了正确的函数名,它仍然只调用第一个函数。

这是下面的具有不同功能的课程,虽然我只包括两个,因为我不想让你不得不搜索大量的代码。

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

无论用户输入什么,它都只调用第一个函数。 我知道这需要处理和欣赏任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

if Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX":表示userattack.upper()等于"FLAMEVORTEX",或字符串"FLAME VORTEX"的值为True

现在由于空字符串为False而非空字符串为TrueUserattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX"始终为True,这不是您的意思。

尝试:Userattack.upper() == "FLAMEVORTEX" or Userattack.upper()=="FLAME VORTEX"