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

43 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Project: Write Your Own Multiplication Quiz
Page 199
"""
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}")