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) – Johnny Mopp Jun 04 '22 at 20:18

1 Answers1

-1

You could adapt the following code to your use:

#import module
import time

t = 10 #set countdown/timer    
while t:
  mins, secs = divmod(t, 60)
  timer = '{:02d}:{:02d}'.format(mins, secs)
  print(f'Remaining time: {timer}')
  time.sleep(1)
  t -= 1

print('Finished!')
Karl Knechtel
  • 56,349
  • 8
  • 83
  • 124
Drakax
  • 657
  • 8
  • 1
    This does not explain how the code is intended to work, and I don't think it's addressing the question, either. It seems that OP intends to receive input *while* the timer is counting down. It's not clear exactly what the specification is, though. – Karl Knechtel Jun 04 '22 at 20:17
  • There's not much explanation to add about the timer code, it's pretty self-explanatory :) The I'm not here to provide the full solution/code, I wan't to give him/her some clues/tips to reach out his/her goal ;) – Drakax Jun 04 '22 at 20:20
  • @Drakax answers aren't meant to be clues/tips see [here](https://stackoverflow.com/help/how-to-answer) the comments are better suited for that. Also this code bears a striking resemblance to [GeeksForGeeks](https://www.geeksforgeeks.org/how-to-create-a-countdown-timer-using-python/)... so you could have just linked them to that page – 0x263A Jun 04 '22 at 22:53