944,219 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1629
  • Python RSS
Sep 29th, 2009
0

Python loop help

Expand Post »
We are creating a loop in my CSET class and I am stuck. This is the original script I created:
python Syntax (Toggle Plain Text)
  1. #This is a game character health simulator
  2. health = int(input("What is your characters health?"))
  3. if health <0:
  4. print("Dead")
  5. elif health >=0 and health <100:
  6. print("Weak")
  7. elif health >100 and health <=500:
  8. print("Healthy")
  9. elif health >500:
  10. print("Strong!")
  11. 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.
python Syntax (Toggle Plain Text)
  1. #This is a game character health simulator
  2. health = int(input("What is your characters health?"))
  3. while health != -1:
  4. if health <=0:
  5. print("Dead")
  6. elif health >=0 and health <100:
  7. print("Weak")
  8. elif health >100 and health <=500:
  9. print("Healthy")
  10. elif health >500:
  11. print("Strong!")
  12. input("Press Enter")
Any help is greatly apperciated.
Last edited by vegaseat; Sep 30th, 2009 at 12:41 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CurtisEClark is offline Offline
12 posts
since Sep 2009
Sep 30th, 2009
0

Re: Python loop help

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

Python Syntax (Toggle Plain Text)
  1. >>> myvar = 0
  2. >>> while myvar != 8:
  3. myvar = int(input("input 8 to stop the loop: "))
  4.  
  5.  
  6. input 8 to stop the loop: 4
  7. input 8 to stop the loop: 23949
  8. input 8 to stop the loop: 8
  9. >>>
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Sep 30th, 2009
0

Re: Python loop help

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Sep 30th, 2009
0

Re: Python loop help

It is much simpler and more readable if you set up your while loop this way ...
python Syntax (Toggle Plain Text)
  1. # This is a game character health simulator
  2. # give the user instructions
  3. print("Character health can be from 0 to 600+ (-1 exits the loop)")
  4. while True:
  5. health = int(input("What is your characters health?"))
  6. if health == -1:
  7. break # exits the while loop
  8. if health <= 0:
  9. print("Dead")
  10. elif health >= 0 and health < 100:
  11. print("Weak")
  12. elif health > 100 and health <= 500:
  13. print("Healthy")
  14. elif health > 500:
  15. print("Strong!")
  16.  
  17. input("Press Enter")
Simply an endless loop with an exit condition.

Note: This is Python3 code
Last edited by vegaseat; Oct 1st, 2009 at 10:20 am. Reason: Python3
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Read a line from a txt file
Next Thread in Python Forum Timeline: Base http server, collect variable value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC