简体   繁体   中英

How to force some user input after 10 seconds

I'm a python starter and need some help on a quiz like game. This is my code:

import time
from threading import Timer
import random as rnd

q = ["q1", "q2", "q3"]
a = ["a1    b1    c1", "a2    b2    c2", "a3    b3    c3"]
ca = ["b", "c", "b"]
points = 0


rand_q = rnd.randint(0, len(q) - 1)                                             # Choosing random question
print(q[rand_q] + "\n" + a[rand_q] + "\n")                                      # Asking question and showing answers
time.sleep(0.5)                                                                 # Little pause between prompts

t = Timer(10, print, ['Time is up!'])                                           # Setting up timer
t.start()                                                                       # Start timer
start = time.time()                                                             # Start of time check
answer = input("You have 10 seconds to choose the correct answer.\n")           # User input
if answer is ca[rand_q]:                                                        # Check if answer is correct
    print("Correct answer!")
    points = (points + round(10 - time.time() + start, 1)) * 10                 # Calculate points
else:
    print("Wrong answer!")
t.cancel()                                                                      # Stop timer
print("Points:", points)
input("Press ENTER to quit")

del q[rand_q]                                                                   # Removing the question
del a[rand_q]                                                                   # Removing the answer
del ca[rand_q]                                                                  # Removing the correct answer

When I run this I can answer questions and get points, but whenver i wait out the timer I get a prompt saying the time is up, but I can still fill in and answer the question.

I want the input to stop working after the 10 seconds, but I can't seem to make this work. Is there any way I can make the timer timeout all previous inputs on top of the "Time is up" prompt.

I've seen more posts like this but they seem outdated and I didn't get them to work.

EDIT: the sleep command doesn't work. It prints a line saying it's too late but you can still enter an answer after. Same for the threading timer. I want to terminate the input command after 10 seconds, but there seems to be no solution for windows.

The problem is that python's input function is blocking, which means the next line of code will not be executed until the user enters some data. A non blocking input is something that a lot of people have been asking for, but the best solution would be for you to create a separate thread and ask the question on there. This question has sort of been answered in this post

This solution will work except the user will still have to press enter at some point to progress:

import time
import threading

fail = False
def time_expired():
    print("Too slow!")
    fail = True

time = threading.Timer(10, time_expired)
time.start()
prompt = input("You have 10 seconds to choose the correct answer.\n")

if prompt != None and not fail:
    print("You answered the question in time!")
    time.cancel()

You can do what you intend to do, but it gets very complicated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM