I am trying to make a countdown timer that prints the time remaining, and when you input something, it prints what you inputted. My problem is I don't want to wait for the input, just keep running the timer. My incorrect code:
timer = 100
while True:
print(timer)
timer -= 1
if input('> '):
print('the output of input')
You could say I want to have the timer printing the time in the background.
Standard input and standard output (accessed via
input()
andprint()
) are not a good choice for writing interactive asynchronous user interface (UI). Python support a few UIs via it's standard library. For example, curses is a text based user interface available on many POSIX systems. Here is an exmaple code for showing a countdown timer while accepting a number from the user:Here's a function that will timeout if no input is given:
You can easily modify it so it shows the remaining time by changing the timeout value on
select.select
to1
, and loopingtimeout
times.