automate-boring-stuff/exercises/ex08/ex08b.py

43 lines
1.2 KiB
Python
Raw Permalink Normal View History

2023-04-20 11:08:00 -06:00
"""
Project: Write Your Own Multiplication Quiz
Page 199
"""
2023-05-11 17:38:12 -06:00
import time
import random
def create_problem():
first = random.randrange(0, 10)
last = random.randrange(0, 10)
return first, last, str(first * last)
def ask(index, total, waiting, tries, a, b, result, ini):
response = input(f"[{index}/{questions}] ¿Cuánto es {a} × {b}?:\n").strip()
if result == response:
fin = time.time() - ini
if fin > waiting:
print("Respuesta correcta pero fuera del límite de tiempo :/")
else:
print("¡Correcto!")
total += 1
else:
tries -= 1
if tries == 0:
print(f"¡Incorrecto! La respuesta es '{result}'.")
else:
print(f"Te quedan {tries} oportunidades.")
total = ask(index, total, waiting, tries, a, b, result, ini)
return total
total, questions, waiting, tries = 0, 10, 8, 3
print(f"Quiz: tienes {waiting}s para contestar cada pregunta")
for i in range(questions):
a, b, result = create_problem()
ini = time.time()
total = ask(i + 1, total, waiting, tries, a, b, result, ini)
time.sleep(1)
print(f"Calificación total: {total}/{questions}")