1

I am creating a sort of a game where the player has to guess a word within a given time limit everything is working accept i dont know how do i run a timer and take input at the same time that too in a loop. I tried some code but it took input then ran the timer and asked input again so one happened after the other and not simultaneously How do i fix it

Here is what i tried

import time
def countdown(seconds):
  while seconds > 0:
    seconds -= 1
    time.sleep(1)
seconds = 6
while seconds > 0:
    input = ("guess word : ")
    countdown(6)

^^ that is only a fraction of my code Full code here - https://sourceb.in/QYk1D9O2ZT

  • 4
    Tip: Have the timer run on a different thread. Have a look into the `threading` module. 1) [Docs linked here](https://docs.python.org/3/library/threading.html). 2) [Tutorial linked here](https://realpython.com/intro-to-python-threading/). – S3DEV Jun 04 '22 at 19:56
  • Looks like this thread suggests how to do exactly what you want [Python multithreading interrupt input()](https://stackoverflow.com/questions/32031762/python-multithreading-interrupt-input) - not only run a timer, but actually stop the `input` function waiting for user to press enter after time has passed. – Lev M. Jun 04 '22 at 20:15
  • Welcome to Stack Overflow. " i dont know how do i run a timer and take input at the same time that too in a loop." What exactly does this mean? Is the timer supposed to make the user wait before typing again, make the user have to type within a time limit, or something else? What should happen when the timer runs out? – Karl Knechtel Jun 04 '22 at 20:15
  • [Python cancel raw\_input/input via writing to stdin?](https://stackoverflow.com/a/19455759) – 001 Jun 04 '22 at 20:18

0 Answers0