Your problem can be solved by using the index
method of the list, which will find the index where the value is inside the list. For example, in the lista = [1, 2, 3, 4]
list, the result of lista.index(3)
would be 2, indicating that number 3 is in position 2 of the list.
So, knowing the highest number of goals, with max(gols)
, you only have to check in which position this value is in the list and fetch the teams in the same position:
>>> max_gols = max(gols)
>>> indice_vencedor = gols.index(max_gols)
>>> time_vencedor = times[indice_vencedor]
Then the value of time_vencedor
would be the name of the team that had the most goals.
However, it is not interesting to keep two values correlated in different structures. Each team has a goal balance, so you better save the information together. In Python, to store correlated data of different types we use the tuple, getting something like ('time A', 10)
or ('time B', 8)
. But as this way it is not very clear what values 10 and 8 represent you can use the named tuple which leaves the code a bit more readable ( for version 3.6 + ):
from typing import NamedTuple
class Time(NamedTuple):
nome: str
pontuação: int
So you can create the team list:
x = int(input('Quantos times? '))
times = []
for i in range(x):
nome = input('Nome: ')
pontuação = int(input('Pontuação: '))
time = Time(nome, pontuação)
times.append(time)
And finally check the winner and the average score:
vencedor = max(times, key=lambda time: time.pontuação)
média = sum(time.pontuação for time in times) / len(times)
Displaying on screen:
print(f'O time vencedor foi {vencedor.nome} com uma pontuação de {vencedor.pontuação}')
print(f'A média das pontuações foi {média}')
Code Running | See in the Repl.it
For any team you can access the nome
and punctuation 'attributes, you can also order the teams, generating the championship classification:
for time in sorted(times, key=lambda time: time.pontuação, reverse=True):
print(f'{time.nome}, com {time.pontuação} pontos')
What would generate an output like:
>>> Quantos times? 3
>>> Nome: Time A
>>> Pontuação: 13
>>> Nome: Time B
>>> Pontuação: 15
>>> Nome: Time C
>>> Pontuação: 10
Time B, com 15 pontos
Time A, com 13 pontos
Time C, com 10 pontos
Code Running | See in Repl.it