The other answers have already explained your mistake, I would just like to add one detail.
Call count
several times is not efficient for what you want to do, since each call of count
scrolls through the list to get the element count (ie you are scrolling through the list 4 times, unnecessarily - in a variation of the Shlemiel the Painter’s Algorithm). In this case, a better option is to use a Counter
, that iterates through the list once and already gets the count of all elements:
votos = []
opcoes = {
'1': 'Windows', '2': 'Linux', '3': 'MacOS', '4': 'Outros'
}
texto_opcoes = '; '.join(f'Vote {i} para {desc}' for i, desc in opcoes.items())
while True:
voto = input(f'Qual é o melhor sistema operacional: {texto_opcoes} (0 para sair):')
if voto == '0':
break
elif voto in opcoes:
votos.append(voto)
else:
print('Opção inválida')
from collections import Counter
c = Counter(votos)
print(f'A quantidade de votos no total foi: {len(votos)}')
for i, desc in opcoes.items():
print(f'A quantidade de votos no {desc} foi: {c[i]}')
print(f'A porcentagem de votos no {desc} foi: {c[i] * 100 / len(votos):.2f}%')
As you can see, I changed other things too. I changed the name of the list that holds the votes of voto
for votos
, because if it will hold more than one vow, it makes more sense that the name is plural. It may seem like a silly and insignificant detail, but giving better names helps a lot when programming.
I made a loop infinite (while True
) to go reading the options, and the stop option is when the entered value is zero: in this case, I use break
to interrupt the loop.
I put the options in a dictionary and used the keys as strings, so you don’t need to convert to int
(because if a number is not entered, it will give error in the program). Because the fact that the keys are numbers is circumstantial, and if you want to change them to something else (for example, "a" for "Windows", "b" for "Linux", etc.), it would be enough to change the keys of the dictionary, and the rest of the code would remain the same.
And I use the same dictionary to show the available options, and also to show the statistics.
Although in this specific case, since dictionary keys are sequential numbers, they could also be in a list. What changes is that the indexes are numbers and start from scratch, so you should check if the conversion to int
worked well, and subtract 1 when saving the vote (in addition to using enumerate
to get indexes by iterating through the list):
votos = []
opcoes = [ 'Windows', 'Linux', 'MacOS', 'Outros']
texto_opcoes = '; '.join(f'Vote {i + 1} para {desc}' for i, desc in enumerate(opcoes))
while True:
try:
voto = int(input(f'Qual é o melhor sistema operacional: {texto_opcoes} (0 para sair):'))
if voto == 0:
break
elif 1 <= voto <= len(opcoes):
votos.append(voto - 1)
else:
print('Opção inválida')
except ValueError:
print('Não foi digitado um número')
from collections import Counter
c = Counter(votos)
print(f'A quantidade de votos no total foi: {len(votos)}')
for i, desc in enumerate(opcoes):
print(f'A quantidade de votos no {desc} foi: {c[i]}')
print(f'A porcentagem de votos no {desc} foi: {c[i] * 100 / len(votos):.2f}%')