Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program: Sorting movies (Lists and Dictionaries) (Python 3) (1) Build a dictiona

ID: 3592839 • Letter: P

Question

Program: Sorting movies (Lists and Dictionaries) (Python 3)

(1) Build a dictionary that contains the movie collection below. Hint: Combine movie title and director into a list.

Year          Title                       Director

2005          Munich                      Steven Spielberg

2006          The Prestige                Christopher Nolan

2006          The Departed                Martin Scorsese

2007          Into the Wild               Sean Penn

2008          The Dark Knight             Christopher Nolan

2009          Mary and Max                Adam Elliot

2010          The King's Speech           Tom Hooper

2011          The Artist                  Michel Hazanavicius

2011          The Help                    Tate Taylor

2012          Argo                        Ben Affleck

2013          12 Years a Slave            Steve McQueen

2014          Birdman                     Alejandro G. Inarritu

2015          Spotlight                   Tom McCarthy

2016          The BFG                     Steven Spielberg

(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:

Enter a year between 2005 and 2016:

2011

The Artist, Michel Hazanavicius

The Help, Tate Taylor

(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:

Enter a year between 2005 and 2016:

2011

The Artist, Michel Hazanavicius

The Help, Tate Taylor

MENU

Sort by:

y - Year

d - Director

t - Movie title

q - Quit

Choose an option:

(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:

...

Choose an option:

y

2005:

    Munich, Steven Spielberg

2006:

    The Prestige, Christopher Nolan

    The Departed, Martin Scorsese

2007:

    Into the Wild, Sean Penn

2008:

    The Dark Knight, Christopher Nolan

...

(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:

...

Choose an option:

d

Adam Elliot:

    Mary and Max, 2009

Alejandro G. Inarritu:

    Birdman, 2014

Ben Affleck:

    Argo, 2012

Christopher Nolan:

    The Dark Knight, 2008

    The Prestige, 2006

...

(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:

...

Choose an option:

t

12 Years a Slave:

    Steve McQueen, 2013

Argo:

    Ben Affleck, 2012

Birdman:

    Alejandro G. Inarritu, 2014

Into the Wild:

    Sean Penn, 2007

...

Explanation / Answer

def main(): movieCollectionDict = {"Munich":[2005, "Steven Spielberg"], "The Prestige": [2006, "Christopher Nolan"], "The Departed": [2006, "Martin Scorsese"], "Into the Wild": [2007, "Sean Penn"], "The Dark Knight": [2008, "Christopher Nolan"], "Mary and Max": [2009, "Adam Elliot"], "The King's Speech": [2010, "Tom Hooper"], "The Help": [2011, "Tate Taylor"], "The Artist": [2011, "Michel Hazanavicius"], "Argo": [2012, "Ben Affleck"], "12 Years a Slave": [2013, "Steve McQueen"], "Birdman": [2014, "Alejandro G. Inarritu"], "Spotlight": [2015, "Tom McCarthy"], "The BFG": [2016, "Steven Spielberg"]} promptForYear = True while promptForYear: yearChoice = int(input("Enter a year between 2005 and 2016: ")) if yearChoice2016: print("N/A") else: for key, value in movieCollectionDict.items(): if value[0] == yearChoice: print(key + ", " + str(value[1]) ) promptForYear = False menu = "MENU" " Sort by:" " y - Year" " d - Director" " t - Movie title" " q - Quit " promptUser = True while promptUser: print(" " + menu) userChoice = input("Choose an option: ") if userChoice == "q": promptUser = False elif userChoice=="y": for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[1], item[0])): print (value[0],end=': ') print(" "+key + ", " + str(value[1])+" ") elif userChoice == "d": for key, value in sorted(movieCollectionDict.items(), key=lambda key_value: key_value[1][1]): print(value[1],end=': ') print(" " + key + ", " + str(value[0])+" ") elif userChoice == "t": for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[0], item[1])): print(key,end=': ') print(" " + str(value[1]) + ", " + str(value[0])+" ") else: print("Invalid input") main()