Answer:
justice_dict = {}
with open("justices.txt", "r") as fh:
for line in fh:
data = line.split(",")
if int(data[-1]) == 0:
continue
yearServed = int(data[-1]) - int(data[-2])
state = data[-3]
president = data[2].split()[-1]
justice = data[0] + " " + data[1]
if state in justice_dict:
current_data = [justice, president, yearServed]
i = 0
inserted = False
for data in justice_dict[state]:
i += 1
if yearServed > data[2]:
inserted= True
justice_dict[state].insert(i, current_data)
break
if not inserted:
justice_dict[state].append(current_data)
else:
justice_dict[state] = [[justice, president, yearServed]]
state = input("Enter a state abbreviation: ")
if state in justice_dict:
print("")
print("Justice\t\tAppointing Pres\t\tYrs Served")
for data in justice_dict[state]:
print(data[0] + "\t" + data[1]+ "\t\t\t" + str(data[2]))
else:
print("No justices have been appointed from the requested state")
Explanation: