Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer the following question in Python. I am getting half points when using the existing code so I will provide that and the errors

Please answer the following question in Python. I am getting half points when using the existing code so I will provide that and the errors I am getting just for more information.

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

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

Errors

image text in transcribed

image text in transcribed

image text in transcribed

Will \& Grace The simpsons Murder, she Wrote Law \& Order Gunsmoked Dallas 4:Compare output 0/2 Enter the input file name: Output differs. See highlights below. Special character legend Input file2.txt 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; Lav \& Order; Law \& Order: Special Victims Unit ER Dallas; Bonanza Cheers; The Jeffersons Will \& Gracet The Simpsons The Jeffersons Smallville Rules of Engagement NYPD Blue Murder, She Wrote Modern Family Mediumd Mama Expected file content Iux Video Theatred Law \& Order: Special Victims Unit Law \& Order Gunsmoked Friends ER Dallas Cheers Castled Bonanzat Barney Miller Wi11 t Graca; Ganali111e, Modern Fan11y? Frienda The B1mpaon. Bulea ot Dnyagenent; Madiun; Iux Videa Theatze; step by stapp Golden a1: Parry Maon; CSI: Wr; The Middia Nets Murdez, She Mrote; wYED Blua, Banea; The Blg Bang thacry Mana; Barnay H111erp Caatia; Partact Stangaza Qanenoka; Iam a Order; Ian 0 odda=: gpacial Vieting tnte Faxs1y Quyd EA, The Jeok Benny Prognan; Rneriean. Dad!; Geay'a Anateny Da11ar; Bonanza; King ot the M111; Banta landing; Arnatrong C1=cle Thea; Cheera; the Jefra=aona Wi11 a Gacat Tha S1mpaonat The M1ddle The Jatteracha The Jack Banny Pacgrant The B1g Bang Thes=y step by stant Sma11v111e Eulas ot Dnyagenant Barry Maxon Pareect Btrangerat MYED B1eE Nets Murdes, Sha Mrotat Madern Fand1y Madium Mana Mad About Yor Lax Videe Theatrat Tan a Ordes: Spacial vuetima Dnit Lan a Oudex Kneta Landingt King of the Hirlit Qunanokar Geey'a Anateny Colden Girlat F=1enda+1 Faxs1y Quy ER. Da11an Cheerat Caatian CgI: NY+ Banea+ Bananxa Ba=ney Mu11a= Aenateong Cirela theatre Rmarican 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

Step: 3

blur-text-image

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

Students also viewed these Databases questions