python - How to make a countdown timer that runs in the background, which terminates the program when it runs out? -


i making wants millionare game in python using graphics. want user 45 seconds per question answer it. however, whenever put timer in code waits 45 seconds first, then lets user answer, instead of running in background , letting user answer @ same time.

using threading module run multiple threads @ once

you use python threading module make 2 things happen @ once, thereby allowing user answer while timer ticks down.

some example code utilizing this:

from threading import thread time import sleep import sys  def timer():     in range(45):         sleep(1)   #waits 45 seconds     sys.exit() #stops program after timer runs out, have print or keep user attempting answer longer  def question():     answer = input("foo?")  t1 = thread(target=timer) t2 = thread(target=question) t1.start() #calls first function t2.start() #calls second function run @ same time 

it's not perfect, code should start 2 different threads, 1 asking question , 1 timing out 45 seconds before terminating program. more information on threading can found in the docs. hope helps project!


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -