Validate a record in a dict list, if found, update, if not, insert (Python)

Viewed 22

I have a minor problem because of 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 valid the dice results, the list takes the name of the player and the values ​​of the dice, 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:

[ 
  {
   'Name': 'luis',
   'Dice': (1, 2, 3, 3, 3),  
   'Bet': 500 
  },  {
    'Name': 'andrew',
    'Dice': (2, 2, 2, 1, 2),
    'Bet': 500 
  } 
]

But I don't know how to do the following:

  1. If the list is empty, without player dictionaries, save the first one based on a previous validation.
  2. Whose 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, like the first point, but it updates the value of the "key" dice to the new values ​​of the dice you receive.

Here is my full code:

import os
import random as r
from colorama import *

os.system('cls')

player_data = []
def validate_values(list_values):
    dice_player_values = list_values[1], list_values[2],  list_values[3],  list_values[4],  list_values[5]
    player = {
            "Name": list_values[0], 
            "Dice": dice_player_values,
            "Bet": 500
        }
    if(1 not in list_values):
        print(Fore.RED + Style.BRIGHT + list_values[0] + ", next, don't have enough look." + Style.RESET_ALL)
        os.system('pause')
        play_dice()
    else:
        # player_data.append(player)
        # next((item for item in player_data if item["name"] == list_values[0]), None)
        for dato in player_data:
            if(dato["Name"] != list_values[0]):
                player_data.append(player)
                continue
            else:
                dato["Name"]["Dice"] = dice_player_values
        contar_unos = list_values.count(1)
        print("It find's: ", str(contar_unos) , ", one.")
        print(player_data)
        os.system('pause')
        play_dice()

def play_dice():
    os.system('cls')
    dice_values = []
    dice_values.clear()
    print(Fore.GREEN + "What's your name? : " + Style.RESET_ALL)
    player_name = input().lower()
    dice_values.append(player_name)

    dice_one = r.randint(1,3)
    dice_values.append(dice_one)

    dice_two = r.randint(1,3)
    dice_values.append(dice_two)

    dice_three = r.randint(1,3)
    dice_values.append(dice_three)

    dice_four = r.randint(1,6)
    dice_values.append(dice_four)

    dice_five = r.randint(1,6)
    dice_values.append(dice_five)
    
    print(dice_values)
    os.system('pause')
    validate_values(dice_values)
    return dice_values

dice = play_dice()
1 Answers

I think your error is where you are checking if there's already a player with a certain name in the list:

for dato in player_data:
     if(dato["Name"] != list_values[0]):
           player_data.append(player)
           continue

This is only checking if the current item in the list has a person with that specific name. You need to check them all at once before you do anything. So you could do something like this:

newPlayer = false
for dato in player_data:
     if dato['Name'] == list_values[0]:
          dato['Dice'] = dice_player_values
          newPlayer = true
          break
if newPlayer == true:
    player_data.append(player)
Related