1

So I am trying to make a math quiz and after the question pops up after 3 or 4 seconds the computer answers for you instead BUT if the player answers within the time limit the computer does not say the answer. I just started coding at school for a project... Thanks in advance

import time
import random
def computer():
time.sleep(5) #number of seconds the computer waits before answering
print (correct_answer)
#====================================================
from random import randint
number_1 = (randint(0,10)) #random number one
number_2 = (randint(0,10)) #random number two
print(number_1, '+', number_2, "="), #(when i tried to to as an input there was an error)
time.sleep(1) #after one second the question pops up
answer = int(input())
correct_answer = (number_1 + number_2)
if answer == correct_answer:
    print ("Correct")
else:
    print ("wrong")
Yo LEE
  • 55
  • 1
  • 5
  • 1
    Since this is python please indent the code correctly so that we can understand the variable scopes. – Bhargav Mar 15 '17 at 11:27

1 Answers1

0

This is actually not so easy, because the input function pauses the execution of the rest of the program as well as time.sleep. You probably need a getch function or class like this to get rid of the input. Then you need a countdown or count-up timer. You can do that by subtracting the start_time from the current time (time.time())

import time

# start_time = current time
start_time = time.time()
timer = 0

while True:
    timer = time.time() - start_time
    print(round(timer, 2))
    if timer > 3:
        print('Too late. Next question.')
        # New start_time = current time, to reset the timer.
        start_time = time.time()
Community
  • 1
  • 1
skrx
  • 19,980
  • 5
  • 34
  • 48