I have a minor problem, per my understanding of how this works. I have a function to play the dice game, then send the results in a list to another function where I validate the dice results, the list takes the player's name and the dice values, it looks like this:
['John', 1, 2, 3, 3, 3]
Then I'm trying to save the record in a dict, to later save it in a new list, where all the information of the players will be, it will look like this:
[
{
'Nombre': 'luis',
'Dados': (1, 2, 3, 3, 3),
'Apuesta': 500
}, {
'Nombre': 'andrew',
'Dados': (2, 2, 2, 1, 2),
'Apuesta': 500
}
]
But I don't know how to do the following:
- If the list is empty, without player dictionaries, save the first one based on a previous validation.
- A validation is that it checks if the "key" Name already has the "value" of the player's name, if so, it does not insert a new one, as the first point, but it updates the value of the "key" given to the new values of the dice you receive.
Here is my complete code:
datos_jugadores = []
def validar_valores(list_valores):
dados = list_valores[1], list_valores[2], list_valores[3], list_valores[4], list_valores[5]
jugador = {
"Nombre": list_valores[0],
"Dados": dados,
"Apuesta": 500
}
if(1 not in list_valores):
print(Fore.RED + Style.BRIGHT + list_valores[0] + ", no has tenido suerte, por favor, cede tu turno." + Style.RESET_ALL)
os.system('pause')
lanzar_dados()
else:
# datos_jugadores.append(jugador)
# next((item for item in datos_jugadores if item["name"] == list_valores[0]), None)
for dato in datos_jugadores:
if(dato["Nombre"] != list_valores[0]):
datos_jugadores.append(jugador)
continue
else:
dato["Nombre"]["Dados"] = dados
contar_unos = list_valores.count(1)
print("Se encontraron: ", str(contar_unos) , " unos.")
print(datos_jugadores)
os.system('pause')
lanzar_dados()
def lanzar_dados():
os.system('cls')
valores_dados = []
valores_dados.clear()
print(Fore.GREEN + "Hola jugador, mide tu suerte, ¿Cuál es tu nombre? : " + Style.RESET_ALL)
nombre_jugador = input().lower()
valores_dados.append(nombre_jugador)
dado_uno = r.randint(1,3)
valores_dados.append(dado_uno)
dado_dos = r.randint(1,3)
valores_dados.append(dado_dos)
dado_tres = r.randint(1,3)
valores_dados.append(dado_tres)
dado_cuatro = r.randint(1,6)
valores_dados.append(dado_cuatro)
dado_cinco = r.randint(1,6)
valores_dados.append(dado_cinco)
print(valores_dados)
os.system('pause')
validar_valores(valores_dados)
return valores_dados
dados = lanzar_dados()
To simplify all those problems you can change the data structure and create a dictionary of dictionaries.
Think of a dictionary of the style:
The player name is the dictionary key
jugadores
and the value is a dictionaryjugador
that you create on each new play. If I play again Pepe python takes care of everything you ask for.To roll the dice you can use a
for
and you save a lot of code.