Question: my code # Type your code here # Get the input file name from the user input _ file = input ( ) # Read

my code
# Type your code here
# Get the input file name from the user
input_file = input()
# Read the input file contents line by line
with open(input_file, 'r') as file:
lines = file.readlines()
# Create a dictionary to store the number of seasons as keys and TV shows as values
data ={}
i =0
# Loop through the lines in pairs (season and show title)
while i len(lines):
# Extract the number of seasons from the current line (convert to integer)
num_seasons = int(lines[i].strip()) # Remove leading/trailing whitespace
# Extract the TV show title from the next line (convert to integer)
tv_show = lines[i +1].strip() # Assuming season and show title come in pairs on consecutive lines
# Check if the number of seasons exists as a key in the dictionary
if num_seasons in data:
# If the season exists, append the TV show title to the list of shows for that season
data[num_seasons].append(tv_show)
else:
# If the season is new, create a new list with the TV show title as the first element
data[num_seasons]=[tv_show]
# Increment the index by 2 to move to the next season and show title pair
i +=2
# Sort the dictionary by keys (number of seasons) in ascending order
sorted_by_seasons = dict(sorted(data.items()))
# Write the sorted results (by seasons) to a file named 'output_keys.txt'
with open('output_keys.txt','w') as file:
for season, shows in sorted_by_seasons.items():
# Join the list of shows for each season with semicolons (;)
show_list_str ='; '.join(shows)
# Write the season number followed by the list of shows separated by semicolons, with a newline
file.write(f"{season}: {show_list_str}
")
# Sort the shows within each season alphabetically (sort values)
for season, shows in data.items():
shows.sort()
# Sort the entire dictionary by show titles while maintaining season order (custom key)
sorted_by_titles = dict(sorted(data.items(), key=lambda item: item[1]))
# Write the sorted results (by show titles) to a file named 'output_titles.txt'
with open('output_titles.txt','w') as file:
for season, shows in sorted_by_titles.items():
for show in shows:
# Write each show title on a new line
file.write(f"{show}
")
print("Results written to output_keys.txt and output_titles.txt")
error i receive is on Results written to output_keys.txt and output_titles.txt
my code # Type your code here # Get the input

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