Question: This almost worked, but I got a few errors. Code below. Question and errors in photos. def main ( ) : # Step 1 :

This almost worked, but I got a few errors. Code below. Question and errors in photos.
def main():
# Step 1: Read input file name from user
input_file = input("Enter the input file name: ")
# Step 2: Read input file and process data
with open(input_file, 'r') as file:
lines = file.readlines()
# Step 3: Parse input data into dictionary
tv_shows ={}
for i in range(0, len(lines),2):
seasons = int(lines[i].strip())
show_title = lines[i +1].strip()
if seasons in tv_shows:
tv_shows[seasons].append(show_title)
else:
tv_shows[seasons]=[show_title]
# Step 4: Sort dictionary by keys (descending order)
sorted_by_keys = dict(sorted(tv_shows.items(), reverse=True))
# Step 5: Output sorted by keys to output_keys.txt
with open('output_keys.txt','w') as keys_file:
for key, shows in sorted_by_keys.items():
shows_list ='; '.join(shows)
keys_file.write(f"{key}: {shows_list}
")
# Step 6: Sort dictionary by values (reverse alphabetical order)
sorted_by_values = dict(sorted(tv_shows.items(), key=lambda item: item[1], reverse=True))
# Step 7: Output sorted by values to output_titles.txt
with open('output_titles.txt','w') as titles_file:
for key, shows in sorted_by_values.items():
for show in sorted(shows, reverse=True):
titles_file.write(f"{show}
")
if __name__=="__main__":
main() Write a program that first reads in the name of an input file and then reads the input file using the file readlines 0 method. The input file
contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input
file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since multiple shows could have the
same number of seasons).
Sort the dictionary by key (greatest to least) and output the results to a file named output_keys.txt. Separate multiple TV shows associated
with the same key with a semicolon (, ordering by appearance in the input file. Next, sort the dictionary by values (in reverse alphabetical
order), and output the results to a file named output_titles.txt.
Ex: If the input is:
file1.txt
and the contents of file1.txt are:
the file output_keys.txt should contain:
and the file output_titles.txt should contain:
Will & Grace
The Simpsons
Murder, She Wrote
Law & Order
Gunsmoke
Dallas
Note: End each output file with a newline, and file1.txt is available to download. Output differs. See highlights below. Special character legend
Input
Your file content Enter the input file name:
Output differs. See highlights below.
Special character legend
Input file3.txt
 This almost worked, but I got a few errors. Code below.

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 Databases Questions!