from collections import defaultdict filename = "tvshows.txt" # replace this with your filename #creating empty dictionary shows = defaultdict(list) #open the file file = open(filename) i = 1 for line in file: line = line.rstrip() #stripping over extra white spaces if i%6 == 1: title = line.upper() #movie titles as keys elif i%6 == 2: msg = line first_aired_code = int(msg[0:2]) first_aired = 0 if 0 <= first_aired_code <= 18: first_aired = 2000+ first_aired_code else: first_aired = 1900+ first_aired_code shows[title].append(first_aired) #appednig first aired year seasons = int(msg[2:4]) last_aired = first_aired + seasons shows[title].append(last_aired) # appending last aired year network = int(msg[4]) network_channel = "" if network == 1: network_channel = "ABC" elif network == 2: network_channel = "CBS" elif network == 3: network_channel = "NBC" elif network == 4: network_channel = "FOX" shows[title].append(network_channel) #appending network channel elif i%6 == 3 or i%6 == 4 or i%6 == 5 or i%6 == 0: shows[title].append(line) i+=1 #Printing the desired output for i in shows: print(i, "aired from", shows[i][0],"to",shows[i][1],"on", shows[i][2]+".") print("It starred",shows[i][3]+",",shows[i][4]+",",shows[i][5]+",","and",shows[i][6]+".") print() print() can space correctly thanks u
from collections import defaultdict
filename = "tvshows.txt"
# replace this with your filename
#creating empty dictionary
shows = defaultdict(list)
#open the file
file = open(filename)
i = 1
for line in file:
line = line.rstrip()
#stripping over extra white spaces
if i%6 == 1:
title = line.upper()
#movie titles as keys
elif i%6 == 2:
msg = line
first_aired_code = int(msg[0:2])
first_aired = 0
if 0 <= first_aired_code <= 18:
first_aired = 2000+ first_aired_code
else:
first_aired = 1900+ first_aired_code
shows[title].append(first_aired)
#appednig first aired year
seasons = int(msg[2:4])
last_aired = first_aired + seasons
shows[title].append(last_aired)
# appending last aired year
network = int(msg[4])
network_channel = ""
if network == 1:
network_channel = "ABC"
elif network == 2:
network_channel = "CBS"
elif network == 3:
network_channel = "NBC"
elif network == 4:
network_channel = "FOX"
shows[title].append(network_channel)
#appending network channel
elif i%6 == 3 or i%6 == 4 or i%6 == 5 or i%6 == 0:
shows[title].append(line)
i+=1
#Printing the desired output
for i in shows:
print(i, "aired from", shows[i][0],"to",shows[i][1],"on", shows[i][2]+".")
print("It starred",shows[i][3]+",",shows[i][4]+",",shows[i][5]+",","and",shows[i][6]+".")
print()
print()
can space correctly thanks u
Step by step
Solved in 2 steps