Question
PYTHON 3.7 The code runs in the command prompt, but does not return the correct outputs. My code is at the very end. I'm including
PYTHON 3.7
The code runs in the command prompt, but does not return the correct outputs.
My code is at the very end. I'm including the link to the csv file for the program to access:
https://drive.google.com/drive/folders/1bLSezhGdnzkbLZeGlKP6zcQK3j9endDx?usp=sharing
def read_votes(filename):
#counter
i = 0
#Declare dictionary name as votes_db
votes_db = {}
#open the file in read mode
#similar to f = open(filename, 'r'), don't forget f.close()
with open(filename, 'r') as f:
header = f.readline().line.strip().split(',')
length = len(data)
#loop to prevent overrun in memory
#not reading in constantly, one line at a time
for line in f:
data = line.strip().split(',')
if data[0] not in votes_db:
votes_db[data[0]]= [(data[1], data[2], int(data[3]), int(data[4]))]
else:
for state in vote_db[data[0]]:
if key == data[1]:
continue
votes_db[data[0]].insert(i, (data[1], data[2], int(data[3]), int(data[4])))
#f.close()
return votes_db
votes.csv file: It contains records of votes in the following format: state, candldate, party, popular_votes, electoral_votes AL, Johnson, IND, 44467, 0 AL, Steln, IND, 9391, 0 AK, Trump, REP, 163387,9 VA, Clinton, DEM, 1981473, 13 Note: the total number of electoral votes in the file is 531 instead of 538 because 7 votes of the electoral college were not given as promised! Candidate: we will use the following representation for a candidate inside our program: a tuple containing these values in this order. Note that name and party are string abbreviations, and popular votes and electoral votes are integers. candidate (name, party, popular votes, electoral_votes) Database1: a "database" of votes can store all candidates' votes for each State. Obviously, a candidate's name may get reused in different States, but never in the same State. Not all candidate names appear in every State. Our database must be a dictlonary whose keys are States, and whose values are lists of candidate values. Only candidates with positlve number of popular votes may be present in a State. Empty lists are not allowed as values. Candidates in the same State must be stored alphabetically by their abbreviated name (hint: use insert instead of append when adding items to the list because you cant use sorting functions). are ose values are lists of candidate values Only candidates votes db 'AL': [(Johnson', 'IND', 44467, 0), ('Steln", 'IND', 9391, 0)], AK': [(Trump', 'REP, 163387,9)], VA: [(CIllnton', "DEM", 1981473, 13)] Functions read votes(filename) Description: It reads votes from a file and creates in memory a dictionary of database1 type Parameters: a filename of a CSV formated votes file as described above Return value: the dictionary object created or False for any kind of error Examples db is a databasel dictionary db False votes.csv file: It contains records of votes in the following format: state, candldate, party, popular_votes, electoral_votes AL, Johnson, IND, 44467, 0 AL, Steln, IND, 9391, 0 AK, Trump, REP, 163387,9 VA, Clinton, DEM, 1981473, 13 Note: the total number of electoral votes in the file is 531 instead of 538 because 7 votes of the electoral college were not given as promised! Candidate: we will use the following representation for a candidate inside our program: a tuple containing these values in this order. Note that name and party are string abbreviations, and popular votes and electoral votes are integers. candidate (name, party, popular votes, electoral_votes) Database1: a "database" of votes can store all candidates' votes for each State. Obviously, a candidate's name may get reused in different States, but never in the same State. Not all candidate names appear in every State. Our database must be a dictlonary whose keys are States, and whose values are lists of candidate values. Only candidates with positlve number of popular votes may be present in a State. Empty lists are not allowed as values. Candidates in the same State must be stored alphabetically by their abbreviated name (hint: use insert instead of append when adding items to the list because you cant use sorting functions). are ose values are lists of candidate values Only candidates votes db 'AL': [(Johnson', 'IND', 44467, 0), ('Steln", 'IND', 9391, 0)], AK': [(Trump', 'REP, 163387,9)], VA: [(CIllnton', "DEM", 1981473, 13)] Functions read votes(filename) Description: It reads votes from a file and creates in memory a dictionary of database1 type Parameters: a filename of a CSV formated votes file as described above Return value: the dictionary object created or False for any kind of error Examples db is a databasel dictionary db FalseStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started