1

I'm having a bit of trouble with my class functions. On my class I have 3 different functions but whenever I call one of the functions outside of the class, it only ever calls the first one despite me typing in the correct function name.

This is the class below with the different functions, although I have only included two as I don't want you to have to search through lots of code.

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

This is the place where I am calling the functions:

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

No matter what the user inputs, it only ever calls the first function. I know this is a lot to process and appreciate any help. Thanks

MrN3MESIS
  • 25
  • 4

1 Answers1

0

if Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX": means is userattack.upper() equal to "FLAMEVORTEX", or does the string "FLAME VORTEX" have True value.

Now since empty strings are False and non-empty strings are True, Userattack.upper() == "FLAMEVORTEX" or "FLAME VORTEX" is always True, and that's not what you meant.

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

James K
  • 3,692
  • 1
  • 28
  • 36
  • 1
    A more idiomatic and more readable way would be `if Userattack.upper() in ["FLAMEVORTEX", "FLAME VORTEX"]` – kylieCatt Aug 30 '16 at 07:47