Python loop help

Reply

Join Date: Sep 2009
Posts: 11
Reputation: CurtisEClark is an unknown quantity at this point 
Solved Threads: 0
CurtisEClark CurtisEClark is offline Offline
Newbie Poster

Python loop help

 
0
  #1
Sep 29th, 2009
We are creating a loop in my CSET class and I am stuck. This is the original script I created:
  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.
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training

Re: Python loop help

 
0
  #2
Sep 30th, 2009
Maybe this will help you see what you have done wrong:

  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. >>>
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Python loop help

 
0
  #3
Sep 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Python loop help

 
0
  #4
Sep 30th, 2009
It is much simpler and more readable if you set up your while loop this way ...
  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
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

Tags
loop

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC