0

With the help of https://stackoverflow.com/a/22391379/9088305 and APT command line interface-like yes/no input? I have been able to build a little script that wait for a while but can be interrupted by pressing enter.

import sys, select, os, time
from distutils.util import strtobool


for i in range(100):
    sys.stdout.write("\r")
    sys.stdout.write("Wait for {:2d} seconds. Press Enter to continue".format(i))
    sys.stdout.flush()
    time.sleep(1)
    if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
        check = 0
        while not check:
            userin = raw_input("Are you sure you want to continue? (yes/no)")
            try:
                bool = strtobool(userin)
                if bool:
                    check = 1
                    break
                else:
                    print("Let's continue then")
                    time.sleep(1)
                    break
            except ValueError:
                    check = 0 
                    print("Choose either yes or no, please")        
            if check == 1:
                break

print "\nDone!"

However, upon execution, it will alway give the question once, with an ""Choose either yes or no, please" after that:

Are you sure you want to continue? (yes/no)Choose either yes or no, please Are you sure you want to continue? (yes/no)

I haven't been able to figure out why this is. Can someone help me out? So that I can just get "Are you sure you want to continue? (yes/no)" once and I can put in yes or no.

Thanks in advance!

  • Not sure that's the problem. Do you want "Are you sure you want to continue? (yes/no)" not to be printed again after "Choose either yes or no"? – tobias_k Feb 08 '18 at 13:02
  • The newline in stdin might not be getting consumed, so `input` takes it without waiting for actual input. See [here](https://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream-in-python) for how to flush it. – internet_user Feb 08 '18 at 13:03
  • 1
    If your goal is to run until the user presses enter, then why are you waiting for user input in the loop? Those are contradictory goals. – Daniel Pryden Feb 08 '18 at 13:10
  • it's mess .You can do it in simple way also by using while loop and using raw_input("").Make a loop and ask question and with condition put your input. – Narendra Feb 08 '18 at 13:11
  • Thank everyone, Yes, I do want "Are you sure you want to continue? (yes/no)" to be printed after "Choose either yes or no" , but it shouldn't print "Choose either yes or no" before I entered anything. internet_user thanks, I'll look into that. @DanielPryden Shortly, I want it not to directly break when hitting enter, mainly because accidently hitting enter would mess up simulation that can take a very long time (so the input is more of a security check). – Easterbeast Feb 08 '18 at 13:26
  • @Narendra Thanks for the input, but I don't completely understand what you mean. Can you clarify please? – Easterbeast Feb 08 '18 at 13:30
  • In that case, you need two loops: an inner loop to keep waiting for an Enter press (while whatever else your program does keeps happening) and then an outer loop to ask the user if they really want to abort. If they don't, you restart the inner loop (waiting for Enter); if they do want to abort, you exit the outer loop and shut down. As it stands, you are trying to mix the two kinds of user input in the same loop and that's not going to work. – Daniel Pryden Feb 08 '18 at 13:50
  • Alternatively, why not just offload the simulation work into a separate thread, and then just have the main thread sit blocking on a `raw_input` call? That's simpler and doesn't require dealing with things like `select`. – Daniel Pryden Feb 08 '18 at 13:51

0 Answers0