0

Whenever I run this program, I can't get the while loop to repeat. It's a simple exercise with classes and I don't know what I am doing wrong.

class Enemy():
    def attack(self):
        enemy_health = 50
        while enemy_health > 0:
            action = input("attack enemy?")
            if action.lower() == "yes":
                print("enemy health dropped by 5")
                enemy_health =- 5
            else:
                print("enemy escaped!")




jaguar = Enemy()
jaguar.attack()

I want the input to repeat until the enemy health is 0. Also, should I include any return statements in here instead of simply subtracting from enemy health? Thank you

3
  • 3
    Change this enemy_health =- 5 to enemy_health -= 5. Commented May 1, 2019 at 16:42
  • 1
    You just set the enemy health to -5, should be enemy_health-=5 or enemy_health=enemy_health-5 Commented May 1, 2019 at 16:42
  • A lot of people seem to be saying the same exact thing. Commented May 1, 2019 at 16:45

4 Answers 4

1

You have a small mistake, this line:

enemy_health =- 5

Should actually be:

enemy_health -= 5

Your original line just sets the health to -5. Easier to see the mistake when you change the spacing:

enemy_health = -5  # same as the first line
Sign up to request clarification or add additional context in comments.

Comments

0

When you used enemy_health =- 5, you're not decreasing the enemy health by 5, you're setting it to - 5. Use enemy_health -= 5.

Comments

0

It's because you have to inverse the operator to -=

Comments

0

You have a typo here:

enemy_health =- 5

This sets enemy_health to -5. What you want to do is take whatever enemy_health is and subtract 5 from it and then store that value back in enemy_health.

You could do that like this: enemy_health -= 5

Or like this: enemy_health = enemy_health - 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.