0

Example being: I am trying to get the print statement from Hero.checkLife to print when I input 1. I did a little testing and it seems like it correctly gets the input but just skips the if/elif statements. Not sure why, any tips would be greatly appreciated. The formatting is correct in python but copying into here didn't go super smoothly.

class Hero:
    life = 200
    def checkLife(self):
        if self.life <= 0:
            print("Oh no! Hero has fallen")
        else:
            print("Hero has " + str(self.life)+ " life!")
    def attackedByEnemy(self):
        self.life -= 45

class Enemy:
    life = 115
    def checkLife(self):
        if self.life <= 0:
            print("Enemy has fallen!")
        else:
            print("Enemy has " + str(self.life)+ " life!")
    def attakedByHero(self):
        self.life -= 55

hero = Hero()
enemy = Enemy()

while Enemy.life > 0:
    action = input("Enter attack. \n 1. Check Hero life \n 2. Check 
    Enemy life \n 3. Attack the enemy \n 4. Attack the hero \n")
    if action == 1:
        hero.checkLife()
    elif action == 2:
        enemy.checkLife()
    elif action == 3:
        enemy.attakedByHero()
    elif action == 4:
        hero.attackedByEnemy()
Bartłomiej
  • 1,068
  • 1
  • 14
  • 23
user255580
  • 49
  • 4

1 Answers1

1

As stated by roganjosh in the comments, the problem here is that the input() function returns a string, not an integer (which is what the if/elif statements are checking for). To solve it, convert the result of the input to an integer like so:

action = int(input("""Enter attack. \n 1. Check Hero life \n 2. Check 
Enemy life \n 3. Attack the enemy \n 4. Attack the hero \n"""))
lyxαl
  • 1,108
  • 1
  • 16
  • 26
  • 1
    Thanks! I completely forgot that it took input as a string and that even if it was a number that it would still be a string. – user255580 Mar 02 '18 at 02:12