Ejercicio 8b
This commit is contained in:
parent
f167dd7412
commit
32e4f0d169
|
@ -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}")
|
||||
|
|
Loading…
Reference in New Issue