6.8 KiB
Python Programming
Unit 3 Lab: Object-Oriented Programming
Overview
Welcome to the Unit 3 lab!
Our goal is that at the end of the Unit 5 lab, you'll have an app that prints out the Rotten Tomatoes rating for any movie a user enters. We're getting closer!
Right now, let's use object-oriented programming concepts to improve our code. Specifically, we'll be using dictionaries and classes.
Deliverables
You're going to continue building this locally from where you left off with the last lab. You'll write all of your code in the same movie_app.py file.
Run the file from the command line to check your work.
Reminder: On your laptop, you can run the file from your command line with the following:
python movie_app.py
Hint: Make sure you are printing something out with the
Requirements:
- You have a
Movieclass. - You have a
Mediaclass. - You have docstrings on each function.
- Your
mainfunction prints:
The movie Back to the Future has a rating of 4
The movie Blade has a rating of 4
The movie Spirited Away has a rating of 4
Then:
-
If
search_or_ratingsis1, your program prints an indented list of movies:Back to the Future Blade Spirited Away -
If
search_or_ratingsis2, your program printsThe rating for Moana is 7.
Directions
Augment the code you wrote for the Unit 2 lab.
Part 1: Docstrings
- First, for each of your existing functions, add docstrings to document what each function does. (It doesn't hurt to add them to all functions, including
main()!).- Remember that a
docstringis made with""". For example, yourmain()could look like this:
def main(): """ Main is the entry point into the program, and it calls into the search or ratings functions depending on what the user decides to do. """- As you go through this lab, update your docstrings and create new ones for new functions. It will help you, and others in the future, keep track of what each function does.
- Remember that a
Part 2: Adding a Class
Part 2a: The Classes
-
First, create a
Mediaclass. This will be the parent class ofMovie, and it itself will inherit from theobjectbase python class. -
The
Mediaclass will take two arguments,publisherandmarket, which are both strings. These strings will set class instance variables,self.publisherandself.market. Whenever this class is instantiated or called, assume values ofUniversal StudiosforpublisherandUSAformarket. -
The
Mediaclass will have one class method,.get_media_info(). This method will print theself.publisherandself.marketclass instance variables to stdout and returnNone. -
Next, create a
Movieclass. This class will inherit from the baseMediaclass. We're going to have several movies, each of which will have a title and a rating. We can use aMovieclass as a scaffold to create many movie objects. Near the top of your file, create a class,Movie, that takes an argument ofobject. YourMovieclass should have three functions:__init__, which will take inself,movie_data(this will be a dictionary containing the title and rating of each movie),publisherandmarket.__init__will set a member variable,movie_data, to equal themovie_datadictionary passed in.publisherandmarketvariables will be passed to theMediaobject, which must also have its__init__()method called using thesuper()builtin function.get_movie_title(), a getter function that returns the value of thetitlekey in themovie_datadictionary.get_movie_rating(), a getter function that returns the value of theratingkey in themovie_datadictionary.
-
Now that we have a
Movieclass with getter functions, we won't needprint_movie_rating()orprint_movie_title(), so delete them.- Then, in your
main()function, set the existingelif search_or_ratings == 2:toprint_single_movie_rating, and setelseto print"Error: Your input must be 1 or 2!".
- Then, in your
-
Now that we have a class, let's make a function that creates
Movieobjects.- Create a function called
return_single_movie_object()that takes two arguments,movie_titleandmovie_rating. - Have it create and return a
Movieobject with those values.
- Create a function called
Part 2b: The Class Objects
Now we can make Movie objects. Let's look at all the places we're currently using regular movie titles. Can we replace some of them with Movie objects?
-
First, let's look at
print_all_ratings. Right now, it loops through a list of movie titles and prints out each one. We want it to makeMovieobjects instead, but we don't have ratings. Let's use placeholders and set each rating to4.- Change
print_all_ratingsto loop through the movie title list and callreturn_single_movie_objecton each title, passing in the title and4in the list for parameters. Then, print out"The movie", title, "has a rating of", rating). Use the object's getter functions for thetitleandrating.
- Change
-
Next, let's look at
print_single_movie_rating. Right now, it prints out the variables formovie_titleandmovie_ratingthat we have declared at the top of the file. However, we're going to want it to print out the rating and title of anyMovieobject so that it's useful to the user when they look for a movie.- Change
print_single_movie_ratingto take in a movie title,movie_query. - Then, create a
Movieobject from the title that's passed in. Inprint_single_movie_rating, callreturn_single_movie_objecton that title, with a rating of7(later, the rating will be accurate; for now, let's hard-code it.) - Now, change the
printstatement to use the getter functions on the newMovieobject. - We're calling
print_single_movie_ratinginmain(), so we'll need to change that to provide an argument. Pass it a title of a movie you like, such as"Moana".
- Change
-
Now that we aren't using
movie_titleandmovie_ratingat the top of the file, you can delete them.
Pro tip: If you get confused as to how all these functions interact, it's helpful to draw it out on a piece of paper. Check back to the Requirements section above for an overview.
Phew. You're done! Awesome job.