Я нашел код, который подсчитывает список значений в файле .txt. Тем не менее, по какой-то причине это будет только первое значение, которое появляется. Поэтому, если файл имеет что-то вроде:
1
2
2
3
2
4
, тогда он будет печатать только:
Final Tally
1:1
The program will automatically shut down in 5 minutes.
Полный код:
vote = input('Enter your vote: ')
file = open('votedata.txt', 'a')
file.write(vote + '\n')
print('The system is adding your vote. The next person can vote in 3 seconds.')
time.sleep(3)
if vote == 'tally':
break
#end of loop, beginning of tally
from collections import defaultdict
frequencies = defaultdict(int)
for number in open('votedata.txt'):
frequencies[int(number)] += 1
for number in sorted(frequencies.keys()):
print(' ')
print('Final Tally:')
print(number, ':', frequencies[number])
print(' ')
print('The program will automatically shut down in 5 minutes.')
time.sleep(300)
Как я могу написать это так что он собирает и подсчитывает каждое целое число?