If instead of using variables you use dictionaries you make life easier, as well as being the most commonly accepted way to solve these cases.The room dictionary simply make the keys the number of the room (1, 2, 3, 4) and as a value you could use a list, but it is better to use sets, the numbers are unique and the sets are considerably more efficient when looking for an element in them (hash tabs).For the balls it would be similar, as key the ball number, as value the list with the four zeros.import random
cuartos = {
1: {32, 15, 19, 4, 21, 2, 25, 17, 34},
2: {6, 27, 13, 36, 11, 30, 8, 23, 10},
3: {5, 24, 16, 33, 1, 20, 14, 31, 9},
4: {22, 18, 29, 7, 28, 12, 35, 3, 26}
}
bolas = {i: [0, 0, 0, 0] for i in range(37)}
ultima_bola = random.randint(0, 36)
for _ in range(1000):
nueva_bola = random.randint(0, 36)
for cuarto, numeros in cuartos.items():
if nueva_bola in numeros:
bolas[ultima_bola][cuarto - 1] += 1
ultima_bola = nueva_bola
You can avoid the second. for modifying the room dictionary, investing it, so that the keys seine the number their value their fourth:import random
cuartos = {
32: 0, 15: 0, 19: 0, 4: 0, 21: 0, 2: 0, 25: 0, 17: 0, 34: 0,
6: 1, 27: 1, 13: 1, 36: 1, 11: 1, 30: 1, 8: 1, 23: 1, 10: 1,
5: 2, 24: 2, 16: 2, 33: 2, 1: 2, 20: 2, 14: 2, 31: 2, 9: 2,
22: 3, 18: 3, 29: 3, 7: 3, 28: 3, 12: 3, 35: 3, 3: 3, 26: 3
}
bolas = {i: [0, 0, 0, 0] for i in range(37)}
ultima_bola = random.randint(0, 36)
for _ in range(1000):
nueva_bola = random.randint(0, 36)
if (cuarto := cuartos.get(nueva_bola)) is not None:
bolas[ultima_bola][cuarto] += 1
ultima_bola = nueva_bola
Example of execution:>>> bolas
{0: [8, 9, 7, 7],
1: [10, 7, 3, 8],
2: [6, 8, 6, 9],
3: [2, 4, 7, 6],
4: [8, 2, 4, 7],
5: [8, 4, 12, 7],
6: [3, 7, 10, 8],
7: [7, 5, 4, 4],
8: [9, 6, 7, 6],
9: [6, 12, 5, 6],
10: [5, 5, 8, 7],
11: [2, 8, 7, 2],
12: [6, 6, 6, 10],
13: [11, 5, 3, 5],
14: [5, 5, 3, 5],
15: [5, 6, 8, 7],
16: [9, 6, 5, 5],
17: [6, 5, 6, 4],
18: [4, 9, 9, 12],
19: [3, 10, 11, 2],
20: [6, 5, 8, 5],
21: [5, 5, 5, 8],
22: [11, 6, 7, 8],
23: [5, 3, 4, 5],
24: [5, 7, 6, 10],
25: [4, 11, 6, 9],
26: [7, 5, 4, 5],
27: [6, 10, 5, 9],
28: [8, 9, 10, 11],
29: [9, 5, 9, 7],
30: [5, 8, 9, 8],
31: [4, 8, 4, 4],
32: [9, 5, 7, 4],
33: [9, 6, 5, 5],
34: [10, 5, 7, 9],
35: [4, 6, 3, 9],
36: [10, 6, 10, 6]}
EditionIn your new approach you have two problems:The main thing is, you never change bola_anterior within the function in each call, so this is always 22. This makes all the balls end in the 22 ball dictionary...The second is that in the print always print the ball 0 (dic_bolas[0])import random
import time
ultimas_bolas = [22, 22]
dic_cuartos = {
1: {32, 15, 19, 4, 21, 2, 25, 17, 34},
2: {6, 27, 13, 36, 11, 30, 8, 23, 10},
3: {5, 24, 16, 33, 1, 20, 14, 31, 9},
4: {22, 18, 29, 7, 28, 12, 35, 3, 26}
}
dic_bolas = {i: [0, 0, 0, 0] for i in range(37)}
def principal():
nueva_bola = random.randint(0, 36)
ultima_bola = ultimas_bolas[-2] # <<<<<<<<<<<<<<<<<<<<<
ultimas_bolas.append(nueva_bola)
for cuarto, numeros in dic_cuartos.items():
if nueva_bola in numeros:
dic_bolas[ultima_bola][cuarto - 1] += 1
print((f"Bola anterior {ultimas_bolas[-2]}. "
f"Ahora a salido el {nueva_bola} y le sumamos +1 al cuarto "
f"correspondiente de la bola anterior {dic_bolas[ultima_bola]}")
)
time.sleep(1)
while True:
principal()