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