Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How can you have vertically downward communication?

Answered: 1 week ago