We are creating a loop in my CSET class and I am stuck. This is the original script I created:

#This is a game character health simulator
health = int(input("What is your characters health?"))
if health <0:
    print("Dead")
elif health >=0 and health <100:
    print("Weak")
elif health >100 and health <=500:
    print("Healthy")
elif health >500:
    print("Strong!")
input("Press Enter")

We are supposed to Use a while loop to let user repeat steps 2 . Use number -1 (negative 1) to stop the loop. This is what I have come up with but I can't get it to work.

#This is a game character health simulator
health = int(input("What is your characters health?"))
while health != -1:
    if health <=0:
        print("Dead")
    elif health >=0 and health <100:
        print("Weak")
    elif health >100 and health <=500:
        print("Healthy")
    elif health >500:
        print("Strong!")
input("Press Enter")

Any help is greatly apperciated.

Recommended Answers

All 3 Replies

Maybe this will help you see what you have done wrong:

>>> myvar = 0
>>> while myvar != 8:
	myvar = int(input("input 8 to stop the loop: "))

	
input 8 to stop the loop: 4
input 8 to stop the loop: 23949
input 8 to stop the loop: 8
>>>

Please use code tags when posting code

You will need to re-input the health inside the loop so that it will ask the user for the health again. I suspect the current version outputs the 'health status' waits for enter and then outputs the same status again.

It is much simpler and more readable if you set up your while loop this way ...

# This is a game character health simulator
# give the user instructions
print("Character health can be from 0 to 600+ (-1 exits the loop)")
while True:
    health = int(input("What is your characters health?"))
    if health == -1:
        break  # exits the while loop
    if health <= 0:
        print("Dead")
    elif health >= 0 and health < 100:
        print("Weak")
    elif health > 100 and health <= 500:
        print("Healthy")
    elif health > 500:
        print("Strong!")

input("Press Enter")

Simply an endless loop with an exit condition.

Note: This is Python3 code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.