### ![](https://ga-dash.s3.amazonaws.com/production/assets/logo-9f88ae6c9c3871690e33280fcf557f33.png) Python Programming # Unit 4 Lab: Carefully Adding a Bit More Logic ## Overview Welcome to the Unit 4 lab! Our goal is that, at the end of the next lab (you're almost there!), your app makes it possible for users to search for a movie and print out either the Rotten Tomatoes rating or the search results. Let's get a little closer. We still have hard-coded values, but in the next lab we'll actually get the real ratings. For now, let's incorporate some intermediate variables and error-catching. ------------ ## 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:* ```python 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 1. Your `get_movie_rating` takes a source argument and prints a message if the source isn't found. 1. Your `Movie`'s `rating` holds a list of dictionaries. 1. Your `if` block in the `main` function is now in a `while` loop that continues until the user puts in a valid input. --- ## Directions Augment the code you wrote for the Unit 2 lab. ### Part 1: Making the Rating More Advanced **Part 1a: The Rating Format** Right now, `search_or_ratings` being `2` calls `print_single_movie_rating`, which creates a `Movie` object with the rating, which is currently hard-coded to `7`. However, we don't want a hard-coded rating, right? We want a Rotten Tomatoes rating — we just don't have it yet. It would be nice to have the hard-coded rating labeled with the source `"hard coded"`. Then, when we add the Rotten Tomatoes rating, we can label that with `"Rotten Tomatoes"`. This way, we can prepare to get the real rating but still have testable code. Instead of being a single integer, let's have the `Movie` `rating` be a list of dictionaries. *(Why? So we can store multiple bits of information.)* Our object will look like this: ```python "Ratings":[ {"Source" : "","Value" : "","Value" : " When you follow these directions, be careful that you don't create an infinite loop! If your program doesn't stop, you can hit `ctrl-c` in the terminal window to interrupt the program. Let's look at our `main` function — particularly, the `if` block. Right now, the function ends after the user inputs anything. However, if they input something wrong, instead of printing an error and ending the program, it'd be a lot nicer to print the error and let them try again. Let's put our `if` block in a `while` loop. Something to note about loops: You can exit them using the keyword `break`. Change your `if` block in `main` to look like this: ```python # We set up an infinite loop (while True) so that we can keep asking the # user the same question until they give us valid input ("1" or "2"). As # soon as a valid input is reached, the appropriate function runs and the # loop is terminated with "break." while True: if search_or_ratings == 1: # If search_or_ratings is 1, call list_search_results(). list_search_results(default_movie_list) ## **NEW LINE HERE** - If the user had a valid choice, leave the while loop break elif search_or_ratings == 2: # If search_or_ratings is 2, call print_movie_rating(). print_single_movie_rating("Moana") ## **NEW LINE HERE** - If the user had a valid choice, leave the while loop break else: # If search_or_ratings is otherwise, give an error. print("Error: Your input must be 1 or 2!") ## NOTICE NO BREAK HERE - The user didn't input something valid, so let's give them another try. ``` Compare your code to ours. Remember, if you set `search_or_ratings` to something other than 1–3, `ctrl-c` will stop the loop. ### Part 3: String Formatting Quickly, let's do one last thing. In `print_single_movie_rating`, let's change the `print` statement to have string formatting. You can just use this code: ```python # Print the rating. Note that we have to escape the quotes around the movie # title because those quotes are inside a string that also uses quotes. print("The rating for \"{0}\" is {1}.".format(my_movie.get_movie_title(), my_movie.get_movie_rating())) ```