this homework is about python, I want the answer of the (B) and (C), thank you!
ID: 3709385 • Letter: T
Question
this homework is about python, I want the answer of the (B) and (C), thank you! . U.S. Senate The file Senate113.txt contains the members of the 113th U.S. Senate- that is, the Senate prior to the November 2014 election. Each record of the file consists of three fields-name, state, and party affiliation. Some records in the file are as follows Richard Shelby, Alabama, R Bernard Sanders, Vermont,I Kristen Gillibrand, New York,D The file RetiredSen. txt contains the records from the file Senate113. txt for senators who left the Senate after the November 2014 election due to retirement, defeat, death, or resignation. Some records in the file are as follows: John Rockefeller,West Virginia,D Tom Coburn,Oklahoma,R Carl Levin, Michigan,D The file NewSen.txt contains records for the senators who were newly elected in November 2014 or who were appointed to fill the seats of senators who left after the November 2014 election. Some records in the file are as follows: Shelly Capito, West Virginia,R Steve Daines, Montana ,R Gary Peters,Michigan,D (a) Write a program that uses the three files above to create the file Senate114.txt that contains records (each consisting of three fields) for the members of the 114th Senate where the members are ordered by state. Use this file in parts (b) (c), and (d) (b) Write a program that determines the number of senators of each party affiliation (c) Write a program that determines the number of states whose two senators have (d) Write a program that asks the user to input a state, and then displays the two sena- See Fig. 5.49 the same party affiliation tors from that state. See Fig. 5.50Explanation / Answer
1)
Code:
#!/usr/local/bin/python3
# script name: createSen.py
# create senate file Senate114.txt from Senate113.txt and RetiredSen.txt
def main():
sen = {}
# opening the Senate113.txt in read to store in dictionary sen
with open("Senate113.txt") as fp:
for line in fp:
line = line.strip()
name = line.split(',')[0]
sen[name] = line
fp.close()
# opening the file RetiredSen.txt to compare Senate file and delete the entries common in both as some of them are retired
with open("RetiredSen.txt") as fp:
for line in fp:
line = line.strip()
name = line.split(',')[0]
if name in sen and line == sen[name]:
del sen[name]
fp.close()
sen_list = []
# storing into list the dictionary of Senator file
for name in sen:
fields = sen[name].split(',')
sen_list.append(fields)
# sorting the list by state
sen_list_sorted = sorted(sen_list, key=lambda x: (x[1]))
# writing to new file Senate114.txt the sorted list
fout = open("Senate114.txt", "w")
for item in sen_list_sorted:
name, state, pa = item
print(name+","+state+","+pa, file=fout)
fout.close()
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 createSen.py
Unix Terminal>
2)
Code:
#!/usr/local/bin/python
# program to determine number of senators of each party
# script name: numSenator.py
def main():
# opening the file Senate114 for reading
with open("Senate114.txt") as fp:
num_sen = {}
for line in fp:
line = line.strip()
# getting name, state, pa from the file
name, state, pa = line.split(',')
# assigning key the appropriate value based on pa code
if pa == 'I':
key = 'Independents'
elif pa == 'R':
key = 'Republicans'
elif pa == 'D':
key = 'Democrats'
# counting how many Independents, Republicans and Democrats are available
if key in num_sen:
num_sen[key] += 1
else:
num_sen[key] = 1
fp.close()
# printing the final output
print "Party Affiliations:"
for pa in num_sen:
print " " + pa + ":" + str(num_sen[pa])
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python numSenator.py
Party Affiliations:
Independents:2
Republicans:42
Democrats:43
Unix Terminal>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.