Question
i get an error message when converting str to int or float : line 39, in main votes_rate = round(float(ballots) / float(elig_votes) *100, 2) ValueError:
i get an error message when converting str to int or float :
line 39, in main votes_rate = round(float(ballots) / float(elig_votes) *100, 2) ValueError: could not convert string to float: 'In 1964, 85.15% of eligible voters registered, and 68.73% voted.'
the cod is :
# main def main(): print(' The voter participation data from a file and processes that data in several different ways') print() # info
year = 0 # num of years year_under_60 = 0 # years under 60% reg = 0 # registers votes ballot = 0 # cast ballots vote_more_80 = 0 # votes more than 80% # input file_name = input('Enter the name of the file you want to read: ')
# open file read_file = open(file_name, 'r') report_name = 'REPORT-' + file_name report = open(report_name, 'w')
line = read_file.readline().rstrip()
# loop
while line != "": year = (line) elig_votes = (read_file.readline().rstrip()) reg_votes = (read_file.readline().rstrip()) ballots = (read_file.readline().rstrip())
# the rate of the eligibal votes and registered votes votes_rate = round(float(ballots) / float(elig_votes) *100, 2) reg_rate = round(float(reg_votes) / float(elig_votes) *100, 2)
# if more than 80% voted
if reg_vote? > 80: vote_more_80 += 1 rege_rate = round((ballots/reg_votes) * 100, 2) # if less than 60% voted if votes_rate < 60: year_under_60 += 1 report.write("In " + str(year) + ", " + str(reg_rate) + \ "% of eligible voters registered, and " + \ str(vots_rate) + "% voted. ") # reporet the file
line = read_file.readline().rstrip() # test condition year += 1 ballots += ballots votes_rate += reg_rate
# average rate of voters ave = round(reg_rate + year , 2)
# rate of more than 80% voters voters_80_rate = round(float(votes_more_80)/ float(year) *100, 2)
print('The total number of years listed: ',year) print('Total ballots in all these years: ',format(ballot,',')) print('Average percentage of eligible voters registered: ',reg_rate,'%') print('Number of years with less than 60% of registered voters casting '\ 'ballots: ',format(year_under_60,'.0f')) print('Percentage of years with more than 80% of registered voters casting') print('ballots: ', format(votes_rate,'.1f'),'%') print('An output file named',report_name ,'has been created.') print()
report.close() main()
Step 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