forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
69 lines (51 loc) · 1.47 KB
/
mouredev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import random
import threading
def random_int(digits) -> int:
return random.randint(0, 10**digits - 1)
def input_with_timeout():
def on_timeout():
print("\n¡El tiempo ha finalizado! Pulsa enter.")
global game_on
game_on = False
timer = threading.Timer(3, on_timeout)
timer.start()
try:
answer = input(f"¿Cuál es el resultado de {num1} {operation} {num2}? ")
finally:
timer.cancel()
return answer
operations = ["+", "-", "*", "/"]
correct_answers = 0
num1_digits = 1
num2_digits = 1
game_on = True
while game_on:
num1 = random_int(num1_digits)
num2 = random_int(num2_digits)
operation = random.choice(operations)
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
while num2 == 0:
num2 = random_int(num2_digits)
result = num1 / num2
result = round(result, 1)
answer = input_with_timeout()
if not game_on:
break
elif answer == str(result):
print("Respuesta correcta!")
correct_answers += 1
if correct_answers % 5 == 0:
if correct_answers % 2 == 0:
num2_digits += 1
else:
num1_digits += 1
else:
print("Respuesta incorrecta!")
game_on = False
print(f"Juego finalizado. Has acertado {correct_answers} cálculos.")