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
inputfile input
# Read the input file contents line by line
with openinputfile, r as file:
lines file.readlines
# Create a dictionary to store the number of seasons as keys and TV shows as values
data
i
# Loop through the lines in pairs season and show title
while i lenlines:
# Extract the number of seasons from the current line convert to integer
numseasons intlinesistrip # Remove leadingtrailing whitespace
# Extract the TV show title from the next line convert to integer
tvshow linesi 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 numseasons in data:
# If the season exists, append the TV show title to the list of shows for that season
datanumseasonsappendtvshow
else:
# If the season is new, create a new list with the TV show title as the first element
datanumseasonstvshow
# Increment the index by to move to the next season and show title pair
i
# Sort the dictionary by keys number of seasons in ascending order
sortedbyseasons dictsorteddataitems
# Write the sorted results by seasons to a file named 'outputkeys.txt
with openoutputkeys.txtw as file:
for season, shows in sortedbyseasons.items:
# Join the list of shows for each season with semicolons ;
showliststr ; joinshows
# Write the season number followed by the list of shows separated by semicolons, with a newline
file.writefseason: showliststr
# 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
sortedbytitles dictsorteddataitems keylambda item: item
# Write the sorted results by show titles to a file named 'outputtitles.txt
with openoutputtitles.txtw as file:
for season, shows in sortedbytitles.items:
for show in shows:
# Write each show title on a new line
file.writefshow
printResults written to outputkeys.txt and outputtitles.txt
error i receive is on Results written to outputkeys.txt and outputtitles.txt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
