#πŸ”’ Stop input() from catching inputs during sleep()

37 messages Β· Page 1 of 1 (latest)

rain cape
#
from time import sleep

print("Prepare for prompt")
sleep(2)
# If the user presses ENTER here, the following input() will catch it later.
fav_food = input("What is your favorite food")
print(fav_food)
vagrant sonnetBOT
#

@rain cape

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

uncut wasp
#

Sorry, STDIN.

rain cape
uncut wasp
#

I was thinking of this, but that's only for STDOUT.

rain cape
#

unfortunate

uncut wasp
#

Sec

#
from io import BytesIO
from time import sleep

class SilenceInput:
    def __init__(self):
        self.old_stdin = None

    def __enter__(self):
        self.old_stdin = sys.stdin
        sys.stdin = BytesIO()

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdin = self.old_stdin


print("Prepare for prompt")

with SilenceInput():
    sleep(5)

fav_food = input("What is your favorite food")
print(fav_food)
#

This may be a tad hacky, but I'm not sure how else to handle this. Basically, it temporarily overwrites sys.stdin to a bytes IO object, then restores it when the with block exits.

rain cape
#

damn

#

thanks

uncut wasp
#

Seems to work for what you need. And you're welcome. I typed that pretty fast though and only tested it once, so you'll want to do some testing of your own and learn about context managers if you haven't already.

rain cape
#

Def will look into them

dreamy linden
#

istr MS-DOS of all things had a program to flush the input buffer, for just this sort of situation

uncut wasp
#

It does.

#
import sys
from time import sleep

class SilenceInput:
    def __enter__(self):
        pass

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdin.flush()


print("Prepare for prompt")
with SilenceInput():
    sleep(5)
# If the user presses ENTER here, the following input() will catch it later.
fav_food = input("What is your favorite food")
print(fav_food)
#

Although at this point, the purpose of the context manager is kind of moot. Might as well just do the flush after sleep inline.

#

Wait, nvm, my testing was flawed. The IDE was masking the issue πŸ€¦β€β™‚οΈ

uncut wasp
# rain cape thanks

Sorry, my way doesn't actually work. Sorry if it lead you down thw wrong path.

rain cape
#

dw, haven't even tried it yet

#

Also Ill learn wrappers anyway

uncut wasp
#

Ya, context managers (which "wrap" code) are generally very useful.

rain cape
uncut wasp
odd steeple
#

You can poll stdout to see how much can be read, then read and ignore that, immediately after the sleep

#

Although, if enter hasn't been pressed, it's still only your terminal that's in control

#

Use curses if you really, really don't want the user to input anything beforehand

vernal nexus
#

or you could do something silly, but probably working, by just timing the time it took to enter something... if it's to fast, just re-prompt the user:

from time import sleep, time

print("Prepare for prompt")
sleep(2)


while True:
    start = time()
    fav_food = input("What is your favorite food?: ")
    if time() - start > 0.1:
        break
    print()

print('your fav food:', fav_food)
vernal nexus
#

not really. no.

#

it only repeats as often as you manage to hit enter in those two seconds, and then it waits normally

uncut wasp
#

Ah, right, nvm.

vernal nexus
#

though you will see the prompt multiple times, but I'd say that's a reasonable compromise.

vagrant sonnetBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.