I'm trying to code a boggle game in console. I want to ask for multiple user inputs for different words until game time runs out, and every word input is stored to a list. Initially the code looks like this:
import time
words = []
time_limit = 5
start_time = time.time()
while time.time() - start_time < time_limit:
word = input("Enter word: ")
words.append(word)
But if you try the code above, the input
functionality persists beyond the time limit and only breaks out of the loop when the user supplies an input. The program is supposed to exit the loop (and stops asking for input) when the time limit is reached and not being dependent with user actions.
Although in actuality I may have to use threading like so:
import time
import threading
def countdown(time_limit):
global on_play
start = time.time()
while time.time() - start < time_limit:
pass
on_play = False
on_play = True
words = []
time_limit = 5
timer = threading.Thread(target=countdown, args=(time_limit,))
timer.start()
start_time = time.time()
while on_play:
word = input("Enter word: ")
words.append(word)
Even then, the problem stays the same.
Here are few references/approaches I've tried but they don't particularly "exits" the loop/input when time runs out:
Use of msvcrt module as answered from Time-Limited Input? In addition to this not exiting the loop/input, it seems msvcrt only works on Windows and not cross-platform.
Use of sys.stdin Based from https://www.geeksforgeeks.org/take-input-from-stdin-in-python/, it calls input
function internally, which poses the same issue.
Other references They mostly resemble my own approach.