0

I'm creating a text based game with multiple stats you have to keep up, such as stamina, health, etc. and I am having trouble with what happens if they go below 0. I know a while-loop would work I could do:

life = 1
while(life > 0):
    print("You are alive!")
    print("Oh no! You got shot! -1 Life")
    life-1
print("You are dead! Game Over!")

But I don't know how to do that with multiple conditions such as stamina, hunger, strength etc.

1
  • what happens if you have life, strength and stamina but hunger reaches 0 or less? Commented Nov 8, 2014 at 20:53

4 Answers 4

1

You can combine them into a single test using min:

while min(life, health, stamina) > 0:
Sign up to request clarification or add additional context in comments.

Comments

1

Since 0 evaluates to False in Python, you can use all:

while all((life, stamina, hunger, strength)):

This will test if all of the names are not equal to zero.

If however you need to test if all of the names are greater than zero (meaning, they could become negative), you can add in a generator expression:

while all(x > 0 for x in (life, stamina, hunger, strength)):

Comments

0

You can always use and and or. For example:

while (life > 0) and (health > 0) and (stamina > 0):

Comments

0

You could put if statements into the while loop that check those stats at the start of each iteration. That way you can handle each event individually.

life = 1
while(life > 0):
    if stamina < 1:
        print "out of stamina"
        break
    if hunger < 1:
        print "You died of hunger"
        break
    print("You are alive!")
    print("Oh no! You got shot! -1 Life")
    life-1
print("You are dead! Game Over!")

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.