3

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.

Noah
  • 31
  • 3
  • 3
    Possible duplicate of [Python 3 Timed Input](https://stackoverflow.com/questions/15528939/python-3-timed-input) – Black Thunder Sep 17 '18 at 13:09
  • https://github.com/dbader/schedule might be useful – iamklaus Sep 17 '18 at 13:10
  • This will help you too: https://stackoverflow.com/questions/2933399/how-to-set-time-limit-on-raw-input – Black Thunder Sep 17 '18 at 13:11
  • @BlackThunder I've tried the first one and it sort of works. Only problem is that you have to wait 10 seconds even if you do input something and I'm trying to make it so you don't have to wait the full 10 seconds. The second one doesn't seem to work for me. When I run it just says process finished without any input or output. I have already translated it to python 3.6 (which I'm using) and didn't help. – Noah Sep 17 '18 at 14:47

1 Answers1

0

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.

Genetical
  • 597
  • 3
  • 15