-1

i am making a script that asks mathematical questions and i was wondering, how would i make a timer? I want to make a timer such that the user has 20 seconds to answer and when the time is up then to say: "Sorry, incorrect." I have tried recording the time when the question is given then subtracting it from the time they answer the question then if its great or equal to 20 to display incorrect, however it doesn't work. Any help will be greatly appreciated.

if op != '*': # != means 'not equal'
    r1 = random.randint (-1, 100) # Randomises a number from -1 to 100
    r2 = random.randint (-1, 100)
    answer = ops.get(op)(r1,r2)
    start = time.time()
    equ = int(input('Q{}: What is {} {} {}?\n'.format(q, r1, op, r2)))
    end = time.time()
    if end - start >= 20:
    print ("Sorry you took too long to answer.")
    elif answer.upper() = 'FRIDAY':
    print 'Correct, today is {}'.format(answer)
else:
    r1 = random.randint(-1,12)
    r2 = random.randint(-1,12)
    answer = ops.get(op)(r1,r2)
    equ = int(input('Q{}: What is {} x {}?\n'.format(q, r1, r2)))
if equ == answer :
    score += 1
    print("Correct, Well done, your score is: " + str(score))
else:
    print("Incorrect, Sorry. Your score is: " + str(score))
pythontamer
  • 109
  • 1
  • 10
  • 1
    "it doesn't work": Please explain, and post more complete source (for example, what's in the loop?). – Scott Hunter Jun 12 '15 at 17:28
  • To give you a useful answer, you'd need to tell us more about the interface. Are you using a dialog box from a GUI toolkit? A console window? In any case, a useful search term for you is 'timeout'. Googling 'input' and 'timeout' yielded a number of helpful pages, e.g. [this](http://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python), which might be a duplicate, but it only works in Linux. – rjonnal Jun 12 '15 at 17:31
  • http://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds – Eric Levieil Jun 12 '15 at 17:34
  • i am using Python IDLE – pythontamer Jun 12 '15 at 17:34
  • You still haven't clarified the manner in which your timer "doesn't work" or "interferes," but if this code appears exactly as your actual code does, then I guess the problem is your indentation. – TigerhawkT3 Jun 12 '15 at 17:51

1 Answers1

0

You can use something like this to check and see if they took longer than 20 seconds to answer:

import time

start = time.time()
answer = raw_input('What day is it?: ')
end = time.time()

if end - start >= 20:
    print 'You took too long to answer'
elif answer.upper() == time.strftime('%A').upper():
    print 'Correct, today is {}'.format(answer)
else:
    print 'That's not correct, sorry'

Edit: I think this applies for what you are trying to accomplish. Hopefully you take this and learn from it.

import time
import random
import operator 
ops = {'+':operator.add,
           '-':operator.sub,
           '*':operator.mul,
           '/':operator.truediv}
i = 1
while True:
    r1 = random.randint(-1,12)
    r2 = random.randint(-1,12)
    op = random.choice(list(ops.keys()))
    answer = ops.get(op)(r1,r2)

    start = time.time()
    equ = int(input('Q{}: What is {} {} {}?\n'.format(i, r1, op, r2)))
    end = time.time()

    if end - start >= 10:
        print('You took too long to answer')
    elif equ == answer:
        print('Correct!')
    else:
        print('Wrong')

    i += 1
MrAlexBailey
  • 5,219
  • 19
  • 30
  • What does answer.upper() do? – pythontamer Jun 12 '15 at 17:34
  • @pythontamer It converts the user's answer to all uppercase, so if they type `Friday`, `friday`, `frIDAy` or any other possible cases, they all get changed to `'FRIDAY'` before I check to see if they were right or not. – MrAlexBailey Jun 12 '15 at 17:35
  • My script asks mathematical questions and i want to add the timer before and after the question is asked however this interferes when my script checks if the user's answer is right or wrong. I will not paste my entire script, in order to avoid people copying... – pythontamer Jun 12 '15 at 17:37
  • @pythontamer Don't be concerned about anyone here copying your script. Without it we can't really give you a complete answer, so you'll have to modify examples like this one to fit your application. Based on what you have above this should be very easily made to do what you want. – MrAlexBailey Jun 12 '15 at 17:40
  • The part in the code you showed me: else: print 'That's not correct, sorry', i want to change it such that it proceeds to the next line etc. – pythontamer Jun 12 '15 at 17:40
  • I have shown my script above, hope you can help me. – pythontamer Jun 12 '15 at 17:44
  • @pythontamer, if you're that concerned about copyright, [SO is the wrong place for you](http://stackexchange.com/legal). You'll need to take a class or read a book and create your program yourself. – TigerhawkT3 Jun 12 '15 at 17:44
  • What does raw_input do? – pythontamer Jun 12 '15 at 17:49
  • That's the Python 2 version of `input()`, which you can ignore as it looks like you're working with Python 3. – TigerhawkT3 Jun 12 '15 at 17:50
  • ah alright. Could someone please help me make a timer. – pythontamer Jun 12 '15 at 17:51
  • Actually, you seem to be using both `print()` function syntax and `print` statement syntax. Please clarify what version of Python you are using, as well as what the actual problem is. – TigerhawkT3 Jun 12 '15 at 17:52
  • Thank you very much Jkdc :D – pythontamer Jun 12 '15 at 18:00