I appreciate any help. I\'ve gotten close but I\'m missing something. PYTHON 3 1
ID: 3849904 • Letter: I
Question
I appreciate any help. I've gotten close but I'm missing something.
PYTHON 3
1) Build a dictionary that contains the movie collection below. Hint: Combine movie title and director into a list.
(2) Prompt the user for a single year and output the movie title(s) and director(s) from that year. Output N/A if the year is invalid. (4 pts)
Ex:
(3) After prompting the user for a year and displaying the title(s) and directors(s) from that year, display a menu. The menu enables a user to display the movies sorted by year, director, or movie title. Each option is represented by a single character.
The program initially outputs the menu, and outputs the menu after a user chooses an option. If an invalid character is entered, continue to prompt for a valid choice. The program ends when the user chooses the option to Quit. Hint: Implement Quit before implementing other options. For this step, the other options do nothing. (1 pt)
Ex:
(4) Implement the sort by year menu option. Note: There is a newline and a tab between the year and the movie title/director. (2 pts)
Ex:
(5) Implement the sort by director menu option. For directors with multiple films on the list, order their films by year. Note: There is a newline and a tab between the director's name and the movie title/year. (3 pts)
Ex:
(6) Implement the sort by movie title menu option. Note: There is a newline and a tab between the movie title and the movie director/year. (2 pts)
Ex:
Explanation / Answer
#import the required modules
from itertools import groupby
import re
#movie class definition
class Movie:
def __init__(self, year, title, director):
self.year = year
self.title =title
self.director = director
# reading from the file
movieList = []
fi = open('movies.txt', 'r' )
while True:
line_str = fi.readline()
if line_str == '':
break
str = re.split(r' +', line_str.rstrip(' '))
movieList.append( Movie(str[0], str[1], str[2]))
# function for sorting, grouping and printing according to class attributes
def sortYear(movieList):
movieList.sort(key=lambda x: x.year)
for key, group in groupby(movieList, lambda x: x.year):
print "%s:" %(key)
for thing in group:
print "%s, %s" % (thing.title, thing.director)
print ""
def sortTitle(movieList):
movieList.sort(key=lambda x: x.title)
for key, group in groupby(movieList, lambda x: x.title):
print "%s:" %(key)
for thing in group:
print "%s, %s" % (thing.director, thing.year)
print ""
def sortDirector(movieList):
movieList.sort(key=lambda x: x.director)
for key, group in groupby(movieList, lambda x: x.director):
print "%s:" %(key)
for thing in group:
print "%s, %s" % (thing.title, thing.year)
print ""
# You can do the other things here, like displaying options and call different functions accordingly.
# sortYear(movieList) , sortTitle(movieList) , sortDirector(movieList)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.