5.7 KiB
Python Programming
Unit 2 Lab: Control Flow
Overview
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.
Deliverables
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!
Requirements:
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_ratingsis equal to1, your app will then printBack to the Future Blade Spirited Away - If
search_or_ratingsis equal to2, your app will then print8. - If
search_or_ratingsis equal to3, your app will then printThe rating for Back to the Future is 8.
Directions
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
mainfunction. From here, we'll call everything else. -
In programming, if you have a
mainfunction, 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_titlethat prints outmovie_title. -
In
main, callprint_movie_title. -
Try running your program. Do both your
printstatements 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. -
Let's do the same for the movie rating. Create a function called
print_movie_ratingthat 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_ratingand move yourprintsentence from Lab #1 into it. Then, callprint_single_movie_ratingfrommain. -
Right now, your
mainfunction has threeprintstatements 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_titleandmovie_rating, create a variable calledsearch_or_ratings. For now, set it to1. -
In
main, let's create anifstatement and move our function calls into it. You can then test this out by settingsearch_or_ratingsto different values. -
If
search_or_ratingsis1, callprint_movie_title. -
Otherwise, if
search_or_ratingsis2, callprint_movie_rating. -
Otherwise, call
print_single_movie_rating. -
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
mainfunction, create a list calleddefault_movie_listand 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_ratingsthat takes in a parametermovie_list. In it, loop through each movie inmovie_listand print"The movie", movie, "has a great rating!"Then, in yourmainfunction, callprint_all_ratingsand pass itdefault_movie_list. Put this above yourifblock; it should happen no matter what. -
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_resultsthat takes a parametermovie_titles. In the function, loop through themovie_titleslist and print each title out with four spaces () in front of it (just so it looks a little nicer). -
Let's think about our new
list_search_resultsfunction. Right now, inmain, ifsearch_or_ratingsis 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 thatifstatement: ifsearch_or_ratingsis set to1, calllist_search_resultswith the argumentdefault_movie_listinstead. -
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.