Question: I have this code for the Was Clinton Right python programming challenge, and I keep getting these errors. I ' m not sure what to

I have this code for the Was Clinton Right python programming challenge, and I keep getting these errors. I'm not sure what to do to fix them. An example of the working code to compare with and use to figure out what's wrong would be perfect.
import csv
import math
def parse_president(file):
president ={}
parties ={}
for line in file:
line = line.strip()
tmp_list = line.split(',')
parties[tmp_list[0]]= tmp_list[1]
slices = tmp_list[2:]
for i in slices:
president[int(i)]= tmp_list[0]
return president, parties
def parse_data(file):
data =[]
line_num =1
for line in file:
if line_num >12:
line = line.strip()
tmp_list = line.split(',')
data.append(tmp_list)
line_num +=1
return data
def get_totals(data, presidents):
output =[]
preoutput ={}
for i in data:
i[0]= int(i[0])
try:
preoutput[presidents[i[0]-1]].append(i[1])
preoutput[presidents[i[0]]].extend(i[1:])
except KeyError:
preoutput[presidents[i[0]]]=[]
preoutput[presidents[i[0]]].extend(i[1:])
for i in preoutput:
k = preoutput[i]
tmp_list[i]
before = int(k[0])
total =0
for j in k:
if j =='':
continue
j = int(j)
total += j - before
before = j
tmp_list.append(total)
output.append(tmp_list)
return output
def print_output(output,parties):
rep_total =0
dem_total =0
print("Republicans:")
for i in output:
if parties[i[0]]== "Republican":
rep_total += i[1]
if i[1]>0:
print(i[0],':', abs(round(i[1]/1000,1)), 'Million created jobs')
else:
print(i[0],':',abs(round(i[1]/1000,1)), 'Millon lost jobs')
print("
Total jobs created:", abs(round(dem_total/1000,1)), "Million jobs" )
print("
----------
")
print("The numbers overall are accurate but the individual numbers are off because of termsending too soon.")
print("This data show that while president clinton's numbers were off, his point still stands.")
def main():
with open('presidents.txt','r') as president_file:
president_file = open('presidents.txt','r')
data_file = open('BLS_private.csv','r')
presidents, parties = parse_president(president_file)
data = parse_data(data_file)
output = get_totals(data, presidents)
print_output(output,parties)
president_file.close()
data_file.close()
main()
These are the error messages I keep getting.
LAST RUN on 4/26/2024,7:13:42 PM
Traceback (most recent call last):
File "/home/codio/workspace/fact_check.py", line 33, in get_totals
preoutput[presidents[i[0]-1]].append(i[1])
KeyError: 1966
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".guides/secure/fact_check_grader.py", line 156, in
main()
File ".guides/secure/fact_check_grader.py", line 117, in main
fact_check = import_fact_check(program)
File ".guides/secure/fact_check_grader.py", line 21, in import_fact_check
return importlib.import_module(package_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/codio/workspace/fact_check.py", line 80, in
main()
File "/home/codio/workspace/fact_check.py", line 75, in main
output = get_totals(data, presidents)
File "/home/codio/workspace/fact_check.py", line 36, in get_totals
preoutput[presidents[i[0]]]=[]
KeyError: 1967

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!