| | |
Python loop help
![]() |
•
•
Join Date: Sep 2009
Posts: 11
Reputation:
Solved Threads: 0
We are creating a loop in my CSET class and I am stuck. This is the original script I created:
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.
Any help is greatly apperciated.
python Syntax (Toggle Plain Text)
#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")
python Syntax (Toggle Plain Text)
#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")
Last edited by vegaseat; Sep 30th, 2009 at 12:41 pm. Reason: added code tags
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
Maybe this will help you see what you have done wrong:
Python Syntax (Toggle Plain Text)
>>> 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 >>>
It is much simpler and more readable if you set up your while loop this way ...
Simply an endless loop with an exit condition.
Note: This is Python3 code
python Syntax (Toggle Plain Text)
# 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")
Note: This is Python3 code
Last edited by vegaseat; Oct 1st, 2009 at 10:20 am. Reason: Python3
May 'the Google' be with you!
![]() |
Similar Threads
- Starting Python (Python)
- I am looking for a help in python while loop (Python)
- Plz help me with HW, Python For Loop (Python)
- My first Python Program, it's too messy (Python)
- newbee: simple python for loop question (Python)
- Comparing Python and C, Part 10, Structures (Python)
Other Threads in the Python Forum
- Previous Thread: Read a line from a txt file
- Next Thread: Base http server, collect variable value
| Thread Tools | Search this Thread |







