Question: Please answer the following question in Python. I have asked this question before but the answer was wrong so I will provide the code and

Please answer the following question in Python. I have asked this question before but the answer was wrong so I will provide the code and the errors that come up, thank you.

Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() 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:

20 Gunsmoke 30 The Simpsons 10 Will & Grace 14 Dallas 20 Law & Order 12 Murder, She Wrote 

the file output_keys.txt should contain:

10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons 

and the file output_titles.txt should contain:

Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace 

Note: End each output file with a newline.

Existing Code:

file_name = input("Enter the input file name: ")

# read the contents of the file into a dictionary seasons_dict = {} with open(file_name, "r") as file: lines = file.readlines() for i in range(0, len(lines), 2): seasons = int(lines[i].strip()) show = lines[i+1].strip() if seasons not in seasons_dict: seasons_dict[seasons] = [show] else: seasons_dict[seasons].append(show)

# sort the dictionary by keys (number of seasons) and write to output_keys.txt sorted_keys = sorted(seasons_dict.keys(), reverse=True) with open("output_keys.txt", "w") as file: for seasons in sorted_keys: shows = "; ".join(seasons_dict[seasons]) file.write(f"{seasons}: {shows} ")

# sort the dictionary by values (TV show names) and write to output_titles.txt sorted_values = sorted(seasons_dict.items(), key=lambda x: x[1], reverse=True) with open("output_titles.txt", "w") as file: for seasons, shows in sorted_values: shows = "; ".join(shows) file.write(f"{shows} ")

Please answer the following question in Python. I have asked this question

before but the answer was wrong so I will provide the code

and the errors that come up, thank you. Write a program that

Please try to fix these errors because I have already submitted this question once before!

Will \& Grace The Simpsons Murder, She Wrote Law \& Order Gunsmoket Dallas Will \& Grace; Smallville; Modern Family; Friends The simpsons Rules of Engagement; Medium; Lux Video Theatre Murder, She Wrote; NYPD Blue Mama; Barney Miller; Castle Gunsmoke; Law \& Order; Law \& Order: Special Victims Unit ER Dallas; Bonanza Cheers; The Jeffersons Will \& Grace; Smallville; Modern Family; Friends The Simpsons Rules of Engagement; Medium; Lux Video Theatre; Step by Step; Colden Gid Perry Mason; CSI: NY; The Middle NCIS Murder, She Wrote; NYPD Blue; Bones; The Big Bang Theory Mama; Barney Miller; Castle; Perfect Strangera Gungmoke; Law s Order; Law \& Order: Special Victime Unit Farily Guy ER; The Jack Benny Program; American Dad!; Grey'g Anatomy Dallag; Bonanza; King of the Hill; Knots Landing; Armatrong Circle Theat Cheers; The Jeffersons Will \& Gracet The Simpgongt The Middle The Jeffergong The Jack Benny Programt The Big Bang Theory Step by Stept Smallvillet Rules of Engagement Perry Masont Perfect Strangerst NYPD Blue NCIS Murder, She Wroter Modern Family Medium Mamat Mad About You Iux Video Theatref Law \& Order: Special Victims Unit Law \& Ordert Knots Landingt King of the Hill Gunamoked Grey's Anatomy Golden Girlat Friendgt Family Guy ER- Dallagh Cheerad Castled CSI: NYH Bonest Bonanzat Barney Millerd Armetrong Circle Theatre American Dad

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!