Counter in a value of a dictionary

0

I am a beginner in programming and am doing an exercise in which I need to count the votes of certain numbers according to the number of the player's shirt. For example, if I type in program 23, 3 times, the number 23 will have 3 votes. I would like to know if there is any way to add the number of votes in the key value.

while numero_camisa != 0:
numero_camisa = int(input('Informe o número da camisa do jogador ou zero para sair: '))
if numero_camisa < 0 or numero_camisa > 23:
    print('Número da camisa inválido, favor informar um número entre 1 e 23.')
votos_atletas[numero_camisa] = #acrescentar o número de votos conforme o número da camisa.

My code so far, stayed like this. Now is the question, is there a way to add the number of votes according to what the user is reporting?

    
asked by anonymous 26.03.2018 / 22:05

2 answers

0

Python has, in addition to the dictionary structure that can be used, a native structure in the collections library for solving this problem: Counter . Just you store all the votes in a list:

from collections import Counter

votos = []

while True:
    numero_camisa = int(input('Número da camisa: '))
    if numero_camisa == 0:
        break
    elif 0 < numero_camisa < 23:
        votos.append(numero_camisa)
    else:
        print('Número inválido')


votacao = Counter(votos)

print(votacao)

See working at Repl.it

If the entry were, for example, [1, 2, 2, 3, 2, 4] , the output would be:

Counter({2: 3, 1: 1, 3: 1, 4: 1})
    
27.03.2018 / 02:43
0

You need to access the current value of the dictionary for the value of numero_camisa and increase this value by 1.

Before you see the answer, try doing it yourself this way. How do you open the current vote value of numero_camisa player? How do you assign value to a variable?

votos_atletas[numero_camisa] = votos_atletas[numero_camisa] + 1
    
26.03.2018 / 22:47