0

I'm switching to Python for a while, and time in this language is something I'm not grasping. I'm trying to make it so the user has x amount of seconds to answer the question before the program decides no input is equal to the wrong answer and moves on to the next question.

I've had a few attempts with different results, none quite doing what I'm after.

import random
import operator
qLimit = 10
oqLimit = 10
score = 0
maxNum = 10
timeLimit = 0
otimeLimit = 5


ops = {
    '+':operator.add,
    '-':operator.sub
}

def generateQuestion():
    x = random.randint(1,maxNum)
    y = random.randint(1,maxNum)
    op = random.choice(list(ops.keys()))
    a = ops.get(op)(x,y)
    print("What is {} {} {}?\n".format(x, op, y))
    return a

def askQuestion(a):
    guess = input("")
        try:
            integer_input = int(guess)
    except ValueError:
        print('Please enter a valid number')
        return
        global score
        if integer_input == a:
            print("Correct!")
            score += 1
        else:
            print("Wrong, the answer is",a)


while qLimit != 0:
    askQuestion(generateQuestion())
    qLimit -= 1
    print ("You have", qLimit, "questions remaining")
    print("Your score is",score)
    if (qLimit == 0):
        break

timeLimit is the variable thats meant to decide how long they have to answer a question, otimeLimit is the variable that timeLimit gets reset back to for the next question

  • Not quite, on that thread the programs ceases to ask another question when the timer runs out but while an input is pending it wont stop. – Matt Newton Mar 24 '15 at 02:27
  • 1
    While an `input` is waiting for user interaction, I think Python stops everything until the user gives the input. – jkd Mar 24 '15 at 02:50

0 Answers0