1

I need some help with code in a text based game I am trying to make. My game uses health, and the code starts off with "while health>0:", and in another point in the game, when health eventually =0, the loop still continues. How do I make the loop end when health=0, without finishing the whole loop.

Here is an example:

health=100
while health>0:
  print("You got attacked")
  health=0
  print("test")

Should the code not be stopping when health=0, and not print "test"? How to I get it to stop when health=0? The code I wrote deducts health based on the users actions, so the times when health=0 can vary. I want to end the code whenever health=0 Any help would be appreciated.

Danyal Rana
  • 33
  • 1
  • 4
  • The `health>0` check only happens at the beginning of each iteration, so `health=0` won't take into affect until after `print('test')` – yampelo Dec 11 '16 at 17:42
  • Yes. Throughout my code i will keep deducting health based on the users actions(their input), and i want to to end whenever health=0, which can change based on what decisions the user makes – Danyal Rana Dec 11 '16 at 17:43
  • You should know whenever a user's health will change, simply add an `if health=0 : break ` statement after each time the user's health changes – yampelo Dec 11 '16 at 17:45
  • I thought of doing that, but i though there would be a more efficient way to do it. Ill do that anyway it shouldnt be a problem. Thank you! – Danyal Rana Dec 11 '16 at 17:47

5 Answers5

4

The condition is only evaluated at the start of each iteration. It does not get checked in the middle of an iteration (e.g. as soon as you set to health to zero).

To explicitly exit the loop, use break:

while health>0:
  ...
  if some_condition:
    break
  ...
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

The break statement, like in C, breaks out of the smallest enclosing for or while loop.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

You should use 'break' statement to come out of the loop

health=100
while health>0:
  print("You got attacked")
  # decrement the variable according to your requirement inside the loop
  health=health-1 
  if health==0:
    break
  print("test")
Manel
  • 176
  • 4
  • 16
0

Cleaner implementation

health = 100
while True:
    if (health <= 0): break
    print ("You got attacked!")
    health = 0
    print ("Testing!")

Outputs:

You got attacked!
Testing!
Ehsan
  • 1,338
  • 14
  • 13
0

In a while loop, you can only get your code to stop if you specify some sort of condition. In this case, health is always greater than 0, so it keeps printing "you got attacked". You need to make the health variable decrease till it gets to 0 in order to print "test". Hence;

  `   health=100
      while health>0:
        print("You got attacked")
        health-=5
        if health==0:
         print("test")
         break`

An alternative could be this also;

    `  health=100
      if health>0:
       print("You got attacked")
      if health==0:
       print("test") `