Please, wait while the prompt loads.
import time
import random
#n_questions = int(input('How many questions?'))
from js import alert, prompt, localStorage, window
n_questions = int(prompt("How many questions?", "5"))
localStorage.setItem("n_questions", n_questions)
list_a = []
list_b = []
for i in range(n_questions):
list_a.append(random.randrange(2,10))
list_b.append(random.randrange(2,10))
deltas = []
correct = 0
wrongs = []
print('============= START')
for i in range(n_questions):
start = time.time()
a = list_a[i]
b = list_b[i]
user_answer = str(prompt(str(a) + ' x ' + str(b) + ' = '))
if user_answer == str(a*b):
result = 'Correct!'
correct += 1
else:
wrongs.append([a,b])
result = 'Incorrect. The correct answer is: ' + str(a*b)
delta = round(time.time() - start, 2)
deltas.append(delta)
alert(result)
print(str(a) + ' x ' + str(b) + ' = ')
print(user_answer)
print(result)
print(str(delta)+' s')
print('=============')
print('============= END')
print('Number of questions: ' + str(n_questions))
alert(str(round(100*correct/n_questions,1))+'%'+' with average response time of: '+ str(round(sum(deltas)/len(deltas),2)) + ' s')
print(str(round(100*correct/n_questions,1))+'%'+' with average response time of: '+ str(round(sum(deltas)/len(deltas),2)) + ' s')
if len(wrongs)>0:
user_answer = str(prompt("Correct the wrong ones? Enter 'yes' or 'no'. >>> "))
if user_answer.lower() == 'yes':
randoms = wrongs
print('============= START #2')
correct = 0
deltas = []
for i in range(len(randoms)):
a = randoms[i][0]
b = randoms[i][1]
start = time.time()
user_answer = str(prompt(str(a) + ' x ' + str(b) + ' = '))
if user_answer == str(a*b):
result = 'Correct!'
correct += 1
else:
result = 'Incorrect. The correct answer is: ' + str(a*b)
delta = round(time.time() - start, 2)
deltas.append(delta)
alert(result)
print(str(a) + ' x ' + str(b) + ' = ')
print(user_answer)
print(result)
print(str(delta)+' s')
print('=============')
print('============= END #2')
print('Number of questions: ' + str(len(wrongs)))
print(str(round(100*correct/len(wrongs),1))+'%'+' with average response time of: '+ str(round(sum(deltas)/len(deltas),2)) + ' s')
alert(str(round(100*correct/len(wrongs),1))+'%'+' with average response time of: '+ str(round(sum(deltas)/len(deltas),2)) + ' s')
print('Refresh the page to play it again.')