As it begins, the first tip is use better names for variables. For example, var18
is a too generic name, which says nothing about the same, maybe pontos
or pontuacao
be better.
Since you are saving the same type of information several times (name and punctuation of several teams), it is best to use a list, instead of having multiple variables. In this case, each element in the list will have the information of a team, and for this we can use tuples.
One way to do it would be:
times = [] # lista dos times
for i in range(12, 19):
nome_time = input(f'Entre com o time da {i}° posição: ')
pontos = int(input(f"pontuação dos últimos 5 jogos do {nome_time}: "))
times.append( (nome_time, pontos) ) # cada time é uma tupla contendo o nome e a pontuação
I used f-strings to format messages, which are available since Python 3.6, but if you are using a previous version, you can switch to:
nome_time = input('Entre com o time da {}° posição: '.format(i))
pontos = int(input("pontuação dos últimos 5 jogos do {}: ".format(nome_time)))
In the code above, times
starts with an empty list ([]
), and I make a for
in the range(12, 19)
(the first number is included and the second not, so it will iterate by the numbers 12, 13, 14, 15, 16, 17 and 18).
Then I read the team information: the name and the score. Note that I switched float
for int
. Use float
makes more sense when you can have numbers with decimals, but football team scores are always whole numbers (there’s no way a team gets 10.47 points).
Then, to add the information in the list, I use the method append
, and as a parameter I pass a tuple containing the name and the score. Note that there is a pair of parentheses that seems redundant, but it is not. Without these parentheses it would be:
times.append(nome_time, pontos)
And this gives error because we are passing two parameters to append
, but he only accepts one. By putting times.append( (nome_time, pontos) )
, I am indicating that what I want to add is (nome_time, pontos)
, which is a tuple containing the name and score of a team.
That is, in the end times
will be a list containing several tuples, each tuple containing the team name and its respective score.
For the missing games, I also traded float
for int
, because the amount of games is also an integer number:
jogos_que_faltam = int(input('Quantidades de jogos que faltam para a competição terminar: '))
And for the calculations, just go through the list of teams and create another list with the result of the accounts:
pf = [] # não entendi o que é "PF", então deixei com esse nome mesmo
for time in times:
# time[0] é o nome do time, time[1] é a pontuação
pf.append( (time[0], (time[1] / 15) * jogos_que_faltam * 3 ) )
Well, I said to give better names to variables, but as I did not understand what it is PF
, I don’t know what would be a better name for her.
Anyway, pf
is also a list, containing the result of the account for each team. I confess that I did not understand the logic of these accounts, but anyway I did the same calculation here.
For each team, I create another tuple containing the name (time[0]
) and the result of the (time[1]
is the score that was read in the first for
, and I use this value to do the math).
At the end of this loop, pf
will be a list, in which each element is a tuple containing the team name and the result of the accounts. To sort it by the result, just do:
resultados = sorted(pf, key=lambda x: x[1])
print(resultados) # imprime [ ('time 1', valor), ('time 2', valor), etc... ]
The second parameter of function sorted
is a lambda indicating what will be used as a criterion to sort the list. In this case, I used x[1]
(as each element x
from the list is a tuple, I take the value that is at position 1, which in this case is the result of the accounts - remembering that the team name is at index zero, and the result of the account is at index 1, so x[1]
).
If you want a list containing only values (without team names), just use map
:
# se quiser somente os valores
resultados_somente_valores = list(map(lambda x: x[1], resultados))
print(resultados_somente_valores)
And if you want a list with only the names of the teams:
# se quiser somente os nomes
resultados_somente_nomes = list(map(lambda x: x[0], resultados))
print(resultados_somente_nomes)
Finally, to know which team was relegated, just take the last value of the list:
# último time
ultimo_time = resultados[-1]
print(ultimo_time)
print(f'time {ultimo_time[0]} foi rebaixado, resultado: {ultimo_time[1]}')
Lists accept negative indexes, then -1
corresponds to the last element in the list (-2
is the penultimate, -3
is the antepenultimate, etc).
Bro, it was too top, perfect! That’s what I was researching msm... Only that gave a little problem... The list ta returning the team with more pts, I tried to change -1 p/ -7, but generates the following error: Traceback (Most recent call last): File "app2.py", line 16, in <module> ultimo_time = results[-7] Indexerror: list index out of range
– Dionatan
@Dionatan Error with -7 is because the list does not have 7 elements. If you want the first of the list, use
resultados[0]
– hkotsubo