K
The return of your function is return ganador, valorMasAlto, this is actually syntactic sugar to return a tuple, that is, equivalent to return (ganador, valorMasAlto).So if you assign to a variable the return of your function, that variable will end up pointing to a tuple of two elements:participantes = [{'nombre': 'Juan', 'tarjeta_bingo': 5, 'puntaje': 15},
{'nombre': 'Laura', 'tarjeta_bingo': 2, 'puntaje': 10},
{'nombre': 'Pedro', 'tarjeta_bingo': 1, 'puntaje': 3},
{'nombre': 'Raquel', 'tarjeta_bingo': 4, 'puntaje': 20}
]
def mostrar_ganador(participantes):
ganador = ''
valorMasAlto = 0
for d in participantes:
if d['puntaje'] > valorMasAlto:
valorMasAlto = d['puntaje']
ganador = d['nombre']
return ganador, valorMasAlto
ganador = mostrar_ganador(participantes)
nombre = ganador[0]
puntos = ganador[1]
print(f"El ganador es {nombre} con {puntos} puntos")
However, you can simply unpack the tuple:nombre, puntos = mostrar_ganador(participantes)
print(f"El ganador es {nombre} con {puntos} puntos")
If you wish, you can use the built-in max to get the dictionary with the most valuable score key:import operator
def mostrar_ganador(participantes):
ganador = max(participantes, key=operator.itemgetter("puntaje"))
return ganador["nombre"], ganador["puntaje"]
nombre, puntaje = mostrar_ganador(participantes)
print(f"El ganador es {nombre} con {puntos} puntos")
basically gets the top dictionary from the list, taking into account to order the class-related value puntaje exclusively. This is the case key, which tells you that for every dictionary in the list you get the value of the key puntaje (operator.itemgetter("puntaje")) and use it to compare that dictionary with others.Now, if you have more than one player with the highest score, the feature only returns one of them. You can fix it in several ways, for example, by modifying the function to return a list of tuplas with the data of all participants with the highest score:participantes = [{'nombre': 'Juan', 'tarjeta_bingo': 5, 'puntaje': 15},
{'nombre': 'Laura', 'tarjeta_bingo': 2, 'puntaje': 20},
{'nombre': 'Pedro', 'tarjeta_bingo': 1, 'puntaje': 3},
{'nombre': 'Raquel', 'tarjeta_bingo': 4, 'puntaje': 20}
]
def mostrar_ganador(participantes):
ganadores = []
max_punt = 0
for participante in participantes:
puntos = participante["puntaje"]
if puntos > max_punt:
max_punt = puntos
ganadores = [(participante['nombre'], puntos)]
elif puntos == max_punt:
ganadores.append((participante['nombre'], puntos))
return ganadores
for nombre, puntos in mostrar_ganador(participantes):
print(f"El ganador es {nombre} con {puntos} puntos")
You could use your original function and apply a filter to the list, but the problem is that we iterated twice about it when we can do it once:_, max_pun = mostrar_ganador(participantes)
ganadores = [(participante["nombre"], participante["puntaje"])
for participante in participantes if participante["puntaje"] == max_pun]
for nombre, puntos in ganadores:
print(f"El ganador es {nombre} con {puntos} puntos")