208k views
1 vote
Write a program that requests a state abbreviation as input and displays the justices appointed from that state. The justices should be ordered by their years served. The output should also display the last name of the appointing president and the length of time served. (Note: For current justices, use 2015 - yrAppointed as their time of service. Otherwise, use yrLeft - yrAppointed.) Also, the program should inform the user if no justices have been appointed from the requested state.

1 Answer

1 vote

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")

Step-by-step explanation:

User Daniel Placek
by
5.2k points