0

In my code I would like to keep prompting the user to enter input, as long as life_status = 0. Life_status is set earlier in the code. I think I need a while loop for this, but I am not sure. How would I execute this as well? I've tried looking at a few resources like W3Schools, but I keep getting errors. Thanks!

if imput in age_up:
    print(age + 1)
    age = age + 1
    time.sleep(2)
elif imput in want_sad:
    print(random.choice(sad))
    time.sleep(2.5)
elif imput in spook_me:
    print(random.choice(spoops))
    time.sleep(2.5)
elif imput in vino:
    print(random.choice(vines))
    time.sleep(2.5)
elif imput in no_job:
    job = 0
    print("You quit your job.")
    time.sleep(2)
elif imput in neckrope:
    print("YOU ARE DEAD")
    exit()
else:
    print("That is not a valid response. Please try again.")
    time.sleep(1)
    imput = input(">>> ") ```
  • you can use a while loop `while life_status:`. `rest of the code – Shijith Nov 13 '19 at 13:54
  • When are you planning to change `life_status`? Because as is, if you'll use that as the condition of the loop, it will never stop. – Aryerez Nov 13 '19 at 13:56

3 Answers3

0

Try the following

while life_status == 0:
   if imput in age_up:
      print(age + 1)
      age = age + 1
      time.sleep(2)
   elif imput in want_sad:
      print(random.choice(sad))
      time.sleep(2.5)
   elif imput in spook_me:
      print(random.choice(spoops))
      time.sleep(2.5)
   elif imput in vino:
      print(random.choice(vines))
      time.sleep(2.5)
   elif imput in no_job:
      job = 0
      print("You quit your job.")
      time.sleep(2)
   elif imput in neckrope:
      print("YOU ARE DEAD")
      exit()
   else:
      print("That is not a valid response. Please try again.")
      time.sleep(1)
      imput = input(">>> ") 

Do note that this would result in an endless loop: the variable life_status does not change inside the loop. However, this is what you are requesting

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
0

It is rather simple to implement such a loop. Put all your code in a while loop like so -

while life_status != 0:
    life_status -= 1
    if imput in age_up:
    # your code goes here

OR

while life_status > 0
    life_status -= 1
    # your code goes here 

OR

while True:
    life_status -= 1
    if life_status == 0:
        break
    # elif

Go through this link. It explains all the control flow statements in Python.

Sushant
  • 3,499
  • 3
  • 17
  • 34
  • Right? Feedback would be appreciated – Tim Stack Nov 13 '19 at 14:03
  • Not mine, but answering a question that's obviously a duplicate earns downvotes sometimes. Technically, questions and answers are supposed to be judged on their individual usefulness and other merits (hence not my downvote), but everyone has the right to vote exactly as they please. – Mad Physicist Nov 13 '19 at 14:03
  • 1
    I think it makes more sense in my opinion to flag the question as duplicate than downvoting answers. The question will get closed if there are enough flags for duplicate. And I understand your point. I was being lazy to find a duplicate so I thought I'd throw my two cents as an answer. – Sushant Nov 13 '19 at 14:05
-2

Initiate imput variable with dummy value and use while with your test condition

imput = ""

while(imput != "test"):
    print(">>>")
    imput = input()
print("input OK")
Clément
  • 1,128
  • 7
  • 21