Firstly, this is homework for my computing science class, but we are allowed to get help on the internet or using any other way we want.
I'm trying to write a program to read in a file with user responses to questions (I.E. an automated handset vote) and then to count the number of votes for each answer for each question. For some questions a handset may vote more than once (change their answer) and only the last vote should be counted.
At the moment, i've managed to read in the file, and store it as a dictionary, but i've got no idea how to count the number of votes for each answer?
The file Responses.txt contains the following data in the following format:
Question_number User_ID Answer
1 23 1
1 18 1
1 28 2
1 24 1
1 12 3
1 26 3
1 29 1
1 23 5
2 23 3
3 17 1
from string import *
responsesfile = open("Responses.txt","r")
def getResponses(responsesfile):
responses = {}
for line in responsesfile:
line = line.strip()
parts = [p.strip() for p in line.split(' ')]
responses[parts[1]] = (parts[0], parts[2])
return responses
responses = getResponses(responsesfile)
Any help is much appreciated.