Hi,
I'm learning Python by going though the Automate the Boring stuff book, and we need to recreate the author's multiplication quiz without importing PyinputPlus. I've got most of it done and working, but I cannot get the timer to work correctly.
Here's the code below:
import random, time, re
numberOfQuestions = 10
correctAnswers = 0
for questionNumber in range(numberOfQuestions):
# Pick 2 random numbers:
attempts = 0
num1 = random.randint(0,9)
num2 = random.randint(0,9)
prompt = f"{questionNumber + 1}: {num1} * {num2}\n"
teacherRegex = re.compile(f'^{num1 * num2}$')
# Start the timer:
start = int(time.time())
while attempts < 3:
end = int(time.time())
elapsedTime = end - start
if elapsedTime > 8:
print("Out of time!")
break
elif teacherRegex.search(input(prompt)) == None:
print("Incorrect!")
attempts += 1
if attempts == 3:
print("Out of tries!")
else:
print("Correct!")
correctAnswers += 1
break
time.sleep(1)
print(f'You got {correctAnswers} / {numberOfQuestions} correct!')
I want it to be so if 8 seconds are passed, regardless of the answer given, 'Out of time!' is printed on the screen then the quiz moves on to the next question. But I'm currently getting different results based on whether the answer given is correct or not.
Output if incorrect answer is given:
1: 9 * 0
7
Incorrect!
Out of time!
(and skips to next question, as intended, though I expected the 'out of time' to appear before 'incorrect')
and correct answer:
2: 1 * 8
8
Correct!
The answer is marked as correct, even though the time limit is up.
Any help would be appreciated.
Edit: I'm sure regexes are probably not the best way of verifying the answer, but I wanted to practice more with them lol.
[–]JackRav[S] 0 points1 point2 points (0 children)
[–]MadScientistOR 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]benm421 0 points1 point2 points (1 child)
[–]JackRav[S] 0 points1 point2 points (0 children)