From 32e4f0d1695322f4474c2b6aca3577a17c80a187 Mon Sep 17 00:00:00 2001 From: perro Date: Thu, 11 May 2023 16:38:12 -0700 Subject: [PATCH] Ejercicio 8b --- exercises/ex08/ex08b.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/exercises/ex08/ex08b.py b/exercises/ex08/ex08b.py index 9306bc1..91df9f1 100644 --- a/exercises/ex08/ex08b.py +++ b/exercises/ex08/ex08b.py @@ -2,3 +2,41 @@ 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}")