Answered step by step
Verified Expert Solution
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
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.
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 eg def tempfunction None: with a docstring below the header and the keyword pass as the function body. Save your template.
Save your template again as bingereviews.py for this lab.
Create two hardcoded 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, hardcode the following two dictionaries for two shows:
show: dict title: "Stranger Things",
"numseasons":
"genre": "Fantasy",
"ratings":
"hashtags": SciFi "Kids", "Gore"
show: dict title: "Brooklyn Nine Nine",
"numseasons":
"genre": "Comedy",
"ratings":
"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 show for example, you would write:
printshowtitle
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
Brooklyn Nine Nine
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 ratings list length:
Show ratings list length:
Show hashtags list length:
Show hashtags list length:
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 intinputPlease enter a rating for from to :
formatshowtitle
showratingsappendrating
printShow ratings:", showratings
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: Liststrhello "world"
bar: Dictstr intfoo: "bar":
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.
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 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 and and returns a floatingpoint 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started