3

I'm trying to create a program that runs in a similar format to the game show Countdown.

I have found various ways to create a timer, most successfully with the time.sleep command and a while loop.

However, the user needs to be able to input a word whilst the timer is going on, otherwise the user could take as long as they want to think of their word after the timer has stopped. Once the timer has stopped, whatever the user has typed in should be taken as their word. I haven't found any kind of solution for this yet as python runs sequentially so it's difficult to have a timer and an input at the same time.

aruisdante
  • 8,875
  • 2
  • 30
  • 37
  • I think you need to use threads [this](http://www.tutorialspoint.com/python/python_multithreading.htm) may be useful – lelloman Mar 02 '15 at 20:51
  • possible duplicate of [How to run a background timer in Python](http://stackoverflow.com/questions/26002497/how-to-run-a-background-timer-in-python) – aruisdante Mar 02 '15 at 20:52
  • The problem with threading suggestions is, you can't easily terminate a "waiting for input" thread from a "watches the clock" thread. – Kevin Mar 02 '15 at 20:52
  • @Kevin the duplicate I linked addresses how to do that, it's trying to solve a similar problem. – aruisdante Mar 02 '15 at 20:55
  • Are you trying to write a console app, or a GUI? – Bryan Oakley Mar 02 '15 at 21:17
  • Do you want [raw_input and timeout](http://stackoverflow.com/q/3471461/4279)? (the best solutions are platform dependant, what is your OS?) If you are using a GUI framework; there is usually a builtin way to do it (specific calls are different but the principle is the same). – jfs Mar 03 '15 at 09:33

1 Answers1

0

This worked for me previously... Uses time.time(). If this isn't what you are looking for maybe check out perf_counter()

import msvcrt
import time

def Countdown():
    p = 3.00
    alarm = time.time() + p
    text = []
    while True:
        n = time.time()
        if msvcrt.kbhit():
            text.append(msvcrt.getche())    
        if n < alarm:
            print(round(alarm - n))
        else:
            print("Time's up!")
            break

Countdown()

Making a countdown timer with Python and Tkinter?

Community
  • 1
  • 1
pmat
  • 43
  • 6
  • 1
    Ok, but how can he, at the same time, use `raw_input` or `input` to get a response from the user, while this is running? – Kevin Mar 02 '15 at 21:03
  • Well anything within the while loop should execute until time runs out. So I would add the input feature in the while loop, probably within if n < alarm: – pmat Mar 02 '15 at 21:17
  • Ok, but then `input` will block, and no other code will execute until the user enters some text and presses Enter. So they could wait a million seconds before pressing enter, and your script wouldn't print "Time's up" until after that million seconds. – Kevin Mar 02 '15 at 21:21
  • Ah you are right. Instead maybe he could add an if statement to see if there is a key press is waiting to be read in using msvcrt.kbhit() – pmat Mar 02 '15 at 21:37
  • @Kevin I edited the code to add that in, could you see if that makes sense? – pmat Mar 02 '15 at 21:50