Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 2 Binge Watch Reviews In this lab, you re going to create a functionally decomposed Python program that reads TV show ratings from a

Lab 2
Binge Watch Reviews
In this lab, youre going to create a functionally decomposed Python program that reads TV show ratings from a file. It will create a list of TV shows, where a dictionary represents each show. The program will display a list of show titles with their average ratings, will prompt the user to add a rating and hashtag to one show, and then will display the updated list of show titles and average ratings.
1. Open your Python template, add a function, save it, and then rename it.
You may wish to add a generic function to your Python template as a reminder of how to construct a function a function header (e.g. def temp_function()-> None:) with a docstring below the header and the keyword pass as the function body. Save your template.
Save your template again as binge_reviews.py for this lab.
2. Create two hard-coded TV show dictionaries.
We will begin by creating part of our data structure and working with it to understand the structure and the syntax of accessing parts of our structure. After you have completed each step that works with the data structure, you can comment out the code but leave it at the bottom of your program for reference if you wish.
In your program, hard-code the following two dictionaries for two shows:
show1: dict ={"title": "Stranger Things",
"num_seasons": 3,
"genre": "Fantasy",
"ratings": [5,4,2,5],
"hashtags": ["SciFi", "Kids", "Gore"]}
show2: dict ={"title": "Brooklyn Nine Nine",
"num_seasons": 8,
"genre": "Comedy",
"ratings": [5,5,1],
"hashtags": ["Crime", "TerryCrews"]}
(It will be easier to follow and complete the lab if you type in the data above. Later you can create your own shows and ratings.) Look at the data structure carefully. What is the same for each show? What is different? Both shows have the same structure, but different data stored in the structure.
To display the title of show1, for example, you would write:
print(show1["title"])
Write code to display the title and number of seasons for each show. Run your program and confirm that it works. Your output should look like this (or better, if you formatted it):
Stranger Things 3
Brooklyn Nine Nine 8
The length of the lists for ratings and hashtags can be zero to any number of ratings or hashtags, so while both ratings and hashtags keys reference list values, the lists might be different lengths. Access the length of the ratings list for each show and display the length. Access the length of the hashtags lists for each show and display the number of hashtags. Print the result. Your output should look like this (or similar, with the list lengths correct):
Show 1 ratings list length: 4
Show 2 ratings list length: 3
Show 1 hashtags list length: 3
Show 2 hashtags list length: 2
The program is going to allow the user to add a hashtag and a rating. Write code to obtain a rating and add it to the ratings list for each show, and obtain a hashtag and add it to the hashtags list for each show. If you get stuck, look at the partial solution below (but try to complete this step without looking at the code first). Display the list of ratings and hashtags for each show to confirm that your code added a rating and a hashtag.
rating: int = int(input("Please enter a rating for {} from 1 to 5: "
.format(show1["title"])))
show1["ratings"].append(rating)
print("Show 1 ratings:", show1["ratings"])
About the annotation:
In the code above, the two dictionary are annotated as simply dict. Thats fine. If you want to show what is stored in a list or dictionary, you can import them from typing, like this:
from typing import List, Dict, TextIO
And then annotate your lists and dictionaries as in these examples, for lists and dictionaries with a single type of data:
foo: List[str]=["hello", "world"]
bar: Dict[str, int]={"foo": 1, "bar": 2}
If a list or dict has multiple types of data, you can just annotate as list or dict, or you can just annotate that way anyway, as thats how lists and dicts are annotated in the text.
You can also annotate file reference variables as TextIO from the typing library, or annotate as in the text.
3. Write a function to calculate an average (mean) show rating
We will now start writing some of the functions that will do the work of this program. The description says, The program will display a list of show titles with their average ratings. We know that data will be stored as in our dictionaries in step 2. Our program needs to print the show
titles and average ratings, but in the dictionary we have the show title and a list of ratings. Can we write a function that will calculate the average rating for any show (one with zero, one, or more ratings)?
Lets write a function that takes a list of ratings (a single list containing zero to many integers between 1 and 5), and returns a floating-point average of the values in the list. (An advantage to functions is that you can focus on one small part of the problem. Note that for this functio

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Office 365 For Beginners 2022 8 In 1

Authors: James Holler

1st Edition

B0B2WRC1RX, 979-8833565759

More Books

Students also viewed these Databases questions