Welcome to the second unit lab! Remember, our goal is that at the end of the fifth lab, you're going to have an app that searches for movies or prints out the Rotten Tomatoes rating for any movie a user enters. Right now, you have a variable to hold a movie title, a variable to hold a movie's rating, and a print statement to show the user. Next, let's set up the functions and control flow to print out the values of our variables.
You're going to continue building this locally from 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 command:
python movie_app.py
Hint: Make sure you are printing something out with the print statement! Otherwise, you won't see any output from running your program!
By the end of this, you will have edited your existing movie_app.py
. At the top, you will have a variable called search_or_ratings
.
Your app will always print :
The movie Back to the Future has a great rating!
The movie Blade has a great rating!
The movie Spirited Away has a great rating!
Additionally:
- If
search_or_ratings
is equal to1
, your app will then printBack to the Future Blade Spirited Away
- If
search_or_ratings
is equal to2
, your app will then print8
. - If
search_or_ratings
is equal to3
, your app will then printThe rating for Back to the Future is 8
.
You'll augment the code you wrote for the Unit 1 lab, so leave your two variable declarations at the top of your program and don't delete the print
statement!
- Our program's going to get pretty complex. Let's have a definite starting point. At the bottom of your program, create one
main
function. From here, we'll call everything else. - In programming, if you have a
main
function, you can set it to automatically run when you start the program. In Python, there's a section of code that does this for us. At the very bottom of your file, put this code:
if __name__ == "__main__":
main()
- Now, let's get started! When a user searches for a movie, your program is going to print a whole list of movie titles. But what if we only need to print one title? Create a function called
print_movie_title
that prints outmovie_title
.- In
main
, callprint_movie_title
. - Try running your program. Do both your
print
statements show up? We won't keep reminding you, but throughout this lab, run your program every few steps to be sure it's doing what it's supposed to.
- In
- Let's do the same for the movie rating. Create a function called
print_movie_rating
that prints outmovie_rating
. Inmain
, below your call forprint_movie_title
, callprint_movie_rating
. - What if a user wants to print the whole sentence? Create a function called
print_single_movie_rating
and move yourprint
sentence from Lab #1 into it. Then, callprint_single_movie_rating
frommain
. Your print statement should read something like:print('The rating for ' + movie_title + ' is ' + movie_rating + '.')
- Right now, your
main
function has threeprint
statements in a row. What if a user doesn't want to print all three? Let's give the user a choice. At the very top of your file, bymovie_title
andmovie_rating
, create a variable calledsearch_or_ratings
. For now, set it to1
.- In
main
, let's create anif
statement and move our function calls into it. You can then test this out by settingsearch_or_ratings
to different values.- If
search_or_ratings
is1
, callprint_movie_title
. - Otherwise, if
search_or_ratings
is2
, callprint_movie_rating
. - Otherwise, call
print_single_movie_rating
.
- If
- In
- Later, we'll have many movies, so for now, let's temporarily hard code a list to use in our program. At the top of your
main
function, create a list calleddefault_movie_list
and set it to["Back to the Future", "Blade", "Spirited Away"]
(or some other movies you like!).- Let's set a way to print these out. Create a function called
print_all_ratings
that takes in a parametermovie_list
. In it, loop through each movie inmovie_list
and print"The movie", movie, "has a great rating!"
Then, in yourmain
function, callprint_all_ratings
and pass itdefault_movie_list
. Put this above yourif
block; it should happen no matter what.
- Let's set a way to print these out. Create a function called
- Later, a user can search for a movie and see a whole list of matching titles. For now, let's just make the function with our default list. Create a function called
list_search_results
that takes a parametermovie_titles
. In the function, loop through themovie_titles
list and print each title out with four spaces (- Let's think about our new
list_search_results
function. Right now, inmain
, ifsearch_or_ratings
is set to1
, we callprint_movie_title
. However, that's when a user is going to want a list of movie titles, not just one movie, right? Change thatif
statement: ifsearch_or_ratings
is set to1
, calllist_search_results
with the argumentdefault_movie_list
instead.- Even though we deleted the call to
print_movie_title
, don't delete the function! We'll use it later, when we only need one movie title. You're done! Test it out to be sure you match the requirements above. Great job.
- Even though we deleted the call to
- Let's think about our new