I'm getting back into programming, so I started a project that rolls a die based on the amount of side's a user inputs, and the amount of times the user wants the die to be rolled. I'm having trouble with a part of the program involving time. When the user doesn't input within ten seconds of being prompted, I want a message to be displayed. If nothing is entered in another ten seconds a message should be displayed, so on and so forth. I'm using python.
Right now most of the program is working, but time is only checked after an input is taken. Thus a user could sit on the input screen infinitely and not get prompted. I'm really stuck on how to simultaneously wait for an input, while checking the amount of time elapsed since being prompted for the input.
def roll(x, y):
rvalues = []
while(y > 0):
y -= 1
rvalues.append(random.randint(1, x))
return rvalues
def waitingInput():
# used to track the time it takes for user to input
start = time.time()
sides = int(input("How many sides does the die have? "))
times = int(input("How many times should the die be rolled? "))
tElapsed = time.time() - start
if tElapsed <= 10:
tElapsed = time.time() - start
rInfo = roll(sides, times)
print("Each side occurs the following number of times:")
print(Counter(rInfo))
waitingInput()
else:
print("I'm waiting...")
waitingInput()
Any advice would be appreciated. I'm looking to improve my coding any way I can, so constructive criticism about unrelated code is welcome.
Situations like this call for a threaded timer class. The python standard library provides one:
import threading
...
def waitingInput():
# create and start the timer before asking for user input
timer = threading.Timer(10, print, ("I'm waiting...",))
timer.start()
sides = int(input("How many sides does the die have? "))
times = int(input("How many times should the die be rolled? "))
# once the user has provided input, stop the timer
timer.cancel()
rInfo = roll(sides, times)
print("Each side occurs the following number of times:")
print(Counter(rInfo))