# voteAMatic # # This ghastly little electronic voting machine could be the # end of democracy as we know it. # # Processing begins by asking the user for the number of # candidates in the election, and then requests the user to # enter the name and party affiliation of each. This information # is stored in two lists. # # In the next phase, the program enters a loop that runs # until a zero is entered. The program solicits a numeric choice # for candidate from the user (voter) and tallies the number # of votes each candidate gets. # # The final phase begins after a zero entry is made. The program # prepares a report showing the number of votes and percentage # for each candidate, and then prints a histogram of the # percentages. # # loadSlate # # given two empty lists, one for candidates and one for party affiliations, # this function askes the election officials to enter the number of # candidates and then their names and parties # # names are space padded to 13 characters # def loadSlate ( name, party ): number = len(name) for i in range (number): name[ i ] = raw_input("Please enter the name of candidate " + str(i + 1) + ": ") party[ i ] = raw_input("Please enter " + name[i] + "'s party: ") for j in range (len(name[i]), 13): name[ i ] = name [ i ] + " " # holdElection # # this function is given a list of candidates, a list of their party # affiliations, and a list for tallying votes. # it solicits votes, tallies them, and returns the total number # of votes cast. # def holdElection(name, party, count): total = 0 # build a prompt prompt = "Vote by entering the number of your chosen candidate.\n\n" for i in range (len(name)): prompt = prompt + str(i + 1) + "\t" + name[i] + " (" + party[i] + ")\n" # get votes print prompt choice = input("Enter your vote: ") while choice > 0: if choice < len(name) + 1: count [choice - 1] = count[choice - 1] + 1 total = total + 1 print prompt choice = input("Enter your vote: ") return total # computeStats # # given the total number of votes cast, the number of votes for each # candidate, and an empty array of percentages, compute the # percent earned by each candidate (round to two) # def computeStats ( total, count, percentage ): for i in range( len (count) ): percentage [i] = round(count[i]*100.0/total,2) # reportResults # # this function is given the names and parties of all the candidates, # and the number of votes and percentage of votes earned by each # # it produces two reports: first a table showing each candidate's name # and the number and percentage of votes earned, and second a # histogram representing the percentage for each candidate # def reportResults ( name, party, count, percent ): print "\n\n=============== Election Results ===============\n" print " Candidate\tParty\tVotes\tPercent\n\n" for i in range(len(name)): print name[i], "\t",party[i],"\t",count[i],"\t",percent[i] print "\n\n======= Histogram =======\n\n" for i in range(len(name)): # print histogram bars print name [i], print "|", "*" * int(round(percent[i])) def main( ): number = input ("Please enter the number of candidates: ") candidate = [" "] * number party = [" "] * number loadSlate (candidate, party) slateSize = len(candidate) voteCount = [0] * slateSize percentage = [0.0] * slateSize totalVotes = holdElection ( candidate, party, voteCount) computeStats ( totalVotes, voteCount, percentage ) reportResults ( candidate, party, voteCount, percentage ) main ( )