Question
Python Code Movie Selector (with dictionaries) In this project assignment, you need to create a version of this program which uses a dictionary to record
Python Code
Movie Selector (with dictionaries)
In this project assignment, you need to create a version of this program which uses a dictionary to record each movie.
Program Design
Your program must contain and use the methods listed below. Feel free to add any parameters in the function definitions. Feel free to add other methods as you see fit.
# (new requirement) load all the movies in the file into a list of dictionaries. Each dictionary keeps the information for one movie. load_movies()
#List all movie titles the total number of movies list_movies()
#Prompt the user for a year and list all movie titles for that year list_by_year()
#Prompt the user for part or all of title and #list all movie titles that contain that substring ignoring case search_by_title()
#Prompt the user for a rating, genre, and maximum length and #list all movie titles that meet the criteria search()
The new function, load_movies() will recursively read each line from the file, prepare a dictionary for the current movie, and append the dictionary to a list of dictionaries called movie_info.
For each dictionary of each movie, you should create the following keys:
Title, Year
Length Rating Action Animation Comedy Drama Documentary Romance
For example, from the dataset, the information of the first movie is:
$ 1971 121 NR 001100
A dictionary below should be built for this movie:
{Title: $, Year:1971, Length:121, Rating:NR, Action: False, Animation: False, Comedy:True, Drama: True, Documentary:False, Romance: False}
After creating a dictionary for a movie, you should append the dictionary to the movie_info list. Your list_movies(), list_by_year(), search_by_title() and search() functions should all use the movie_info for the search. In other words, you should only read the file into the main memory once in the load_movies() function.
Study the code below which creates two dictionaries, appends them to a list, and uses a for loop to visit those dictionaries:
items_to_buy = [] # create a dictionary to record the budget for one item dict1= {} |
dict1["budget"]=500 dict1["item_name"] = "washer" items_to_buy.append(dict1) # create a dictionary for another item dict2={} dict2["budget"]=800 dict2["item_name"] = "couch" items_to_buy.append(dict2) print(items_to_buy) # [{'budget': 500, 'item_name': 'washer'}, {'budget': 800, 'item_name': 'couch'}] # create a for loop to visit each dictionary in the list for item in items_to_buy: print("I have {0} budget to buy a {1}".format(item["budget"], item["item_name"])) |
Requirements
Ever have trouble figuring out what movie to watch? This program will allow you to choose from over 58,000 movies with the earliest dating back to the 1890s!
For this assignment, you will implement the requirements defined below. Your program must be named MovieSelector.
File Format
Information about the movies is provided in a movie list file, read at program startup. We're providing an example movie list file with a lot of movies, movies.txt. This particular list is a modified version of the movies file previously found on the web. Of course, your program should work with any movie list file that's properly formatted. The movie list consists of a series of movie descriptions, one per line. Each movie description consists of the five tab-delimited fields listed below. Here, "tab-delimited," means there is a tab character between each field on each line of the file. The first line of the file is a header record with column-names. Your program should just skip over this header. The remaining lines have five fields in the order shown below.
Title (may contain spaces)
Year
Length (in minutes)
Rating (G, PG, PG-13, R, NC-17, NR). NOTE that G is included in the list of
ratings for completeness, but for some reason none of the movies in the input file
actually have this rating.
Genre (Action/Animation/Comedy/Drama/Documentary/Romance) The genre(s)
of the movie are provided as a 6 digit string of 0's and 1's, where a 1 in a given position specifies that the movie fits that genre and a 0 specifies that it does not. For example, the string "011000" denotes that the movie genres are Animation and Comedy (but not Action, Drama, Documentary, nor Romance).
User Input/Error Handling
The program shall continually prompt the user with the following menu until they desire to quit.
Movie Selector - Please enter an option below. L - List all movies Y - List movies by year
T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program
Options:
The program shall accept and use both upper and lower case single letters (l, L, y, Y, t, T, s, S, q, Q) for the options. If the user enters an invalid option, the program shall output an error message and redisplay the menu.
The output shown below for the various options assumes the use of the provided movies.txt file. The output would be different if a different movies list file were used. In some cases, there may not be any output for a particular option depending on the movie list file and the value(s) entered by the user for the option. See the Y - List movies by year option below for an example of a case with no output.
Option: L - List all movies
When selected, this option shall display all of the movie titles, in the same order as they occur in the input file, followed by the total number of movies in the file -- replace the phrase in italics below with the actual number of movies:
$ $1000 a Touchdown $21 a Day Once a Month $40,000
... Zzikhimyeonjukneunda Zzim Zzyzx
Number of movies: total number of movies in the file
Option: Y - List movies by year
When selected, this option shall prompt the user for a year between 1880 and 2050 inclusive and then output all of the movie titles for that year, in the same order as they occur in the input file:
Year (1880-2050): 1897
Admiral Cigarette Biter Bit, The
Bowery Waltz Buffalo Fire Department in Action Cattle Driven to Slaughter Mr. Edison at Work in His Chemical Laboratory Niagara President McKinley at Home Seminary Girls
Movie Selector - Please enter an option below.
L - List all movies Y - List movies by year T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program
Option: y Year (1880-2050): 1885
Movie Selector - Please enter an option below. ...
If the year is not an integer between 1880 and 2050 inclusive, output an error message and redisplay the menu:
Year (1880-2050): 78 Invalid year
Movie Selector - Please enter an option below.
L - List all movies Y - List movies by year T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program
Option: T - Search by title
When selected, this option shall prompt the user for a title or portion of a title and list all movie titles that contain the string, disregarding case, entered by the user. Any spacing entered by the user should be included in the search string. If no movies match the search string, there will not be any output for the search.
Title (is/contains): 24 Hours
24 Hours 24 Hours in London Hey Arnold!: 24 Hours to Live WTC the First 24 Hours
Movie Selector - Please enter an option below.
L - List all movies Y - List movies by year T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program
Option: t Title (is/contains): 24 Hours
Movie Selector - Please enter an option below.
L - List all movies Y - List movies by year T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program
Option: t Title (is/contains): probably not a movie
Movie Selector - Please enter an option below.
L - List all movies Y - List movies by year T - Search by title
S - Search by genre, rating, and maximum length Q - Quit the program
Option: T Title (is/contains): hours
24 Hours 24 Hours in London 36 Hours 36 Hours After Hours After Hours After Office Hours ... Twelve Hours to Kill Twenty-Four Hours to Kill Visiting Hours WTC the First 24 Hours
Option: S - Search by genre, rating, and maximum length
When selected, this option shall prompt the user for a genre (Action(A), Animation(N), Comedy(C), Drama(D), Documentary(O), Romance(R)), rating (G, PG, PG-13, R, NC-17, NR), and a maximum length (as an integer).
Genre (Action(A), Animation(N), Comedy(C), Drama(D), Documentary(O), Romance(R)): O Rating (G, PG, PG-13, R, NC-17, NR): pg Maximum length (min): 70
Ghosts of the Abyss NASCAR 3D: The IMAX Experience Race to Save 100 Years, The Ultimate X: The Movie
This option should work for both upper and lower case. If the user enters an invalid genre, rating, or a maximum length < 1, an error message shall be output and control returned to the menu.
Genre (Action(A), Animation(N), Comedy(C), Drama(D), Documentary(O), Romance(R)): x
Invalid genre Movie Selector - Please enter an option below.
L - List all movies Y - List movies by year T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program
Option: s
Genre (Action(A), Animation(N), Comedy(C), Drama(D), Documentary(O), Romance(R)): r Rating (G, PG, PG-13, R, NC-17, NR): XXX Invalid rating
Movie Selector - Please enter an option below.
L - List all movies Y - List movies by year T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program
Option: s
Genre (Action(A), Animation(N), Comedy(C), Drama(D), Documentary(O), Romance(R)): r Rating (G, PG, PG-13, R, NC-17, NR): NR Maximum length (min): 0
Invalid length Movie Selector - Please enter an option below.
L - List all movies Y - List movies by year T - Search by title S - Search by genre, rating, and maximum length Q - Quit the program
Option:Q - Quit the program
This option will print "Goodbye!" to the user and exit the program.
Program Design
Your program must contain and use the methods listed below. Feel free to add any parameters in the function definitions. Feel free to add other methods as you see fit.
#List all movie titles the total number of movies list_movies()
#Prompt the user for a year and list all movie titles for that year list_by_year()
#Prompt the user for part or all of title and #list all movie titles that contain that substring ignoring case search_by_title()
#Prompt the user for a rating, genre, and maximum length and #list all movie titles that meet the criteria search()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started