Due: 10:00pm EST on Monday, November 2, 2026.
Instructions: complete this homework using Thonny, because hw7 requires working with external files (and that can be cumbersome in Colab).
Click here to download the TVshows folder to get started. This folder contains text files that you can use as input files to analyze. Inside the TVshows folder, create a new file called username_hw7_tvshows.py. For example, this is how Beyoncé Knowles would name her file: bk101_hw7_tvshows.py. Write all your code for this assignment, including your testing code, in this file.
Work with text files, reading from and writing to them
Analyze the provided data and produce a written report
This program reads in a text data file, analyzes the data, and produces a written report in a new file.
The goal of the assignment is to practice:
Reading from a text file
Writing a new text file
Parsing the data in a given text file
You will write a total of four functions to perform the data analysis and write the report.
watched0.txt
At left, see the sample contents of an input file, called watched0.txt. Each line represents a show watched, and the number indicates the minutes watched. This is a very simple file, note that in other viewing files, there can be many more shows, and the same show can be viewed multiple times.
You will calculate these statistics:
the total number of episodes watched
the total minutes of viewing time across all episodes
the longest episode watched
the number of unique shows viewed
report0.txt
At left, see the sample contents of the written report, to a file called report0.txt.
This function takes in the same of the text file containing viewer information. It returns a list of lists, where each sublist corresponds to one line in the given file. In each line, the terminating newline has been removed. The first item is the show name as a string, and the second item is the integer number of minutes viewed. The format looks like this:
[ ['Stranger Things', 22],
['Love Island', 45],
...
]
Hint: the linesFromFile function from lecture is a useful starting point for your dataFromFile function.
This function takes the name of the viewing file to be read. It uses dataFromFile to read the data from the given file, and then returns a four-element tuple containing the following information:
the total number of episodes watched
the total minutes of viewing time across all episodes
the longest episode watched
the number of unique shows viewed
Use the provided testCase function to test that your analyzeData works correctly in multiple cases.
>>> analyzeData('watched0.txt')
(3, 79, 30, 3)
>>> analyzeData('watched1.txt')
(6, 185, 48, 3)
>>> analyzeData('watched2.txt')
(11, 471, 55, 5)
>>> analyzeData('watched3.txt')
(92, 3092, 61, 17)
>>> analyzeData('watched4.txt')
(50, 2147, 89, 50)
▶️ Click to see the test case function that you can use to test analyzeData
def testCase(actual, expected, description=""):
"""
Simple testing helper for students
Parameters:
actual: The result from the student's code
expected: The expected correct result
description: Optional explanation of the test
"""
print(f"TEST: {description}")
print(f"Function returned : {actual}")
print(f"Expected result : {expected}")
if actual == expected:
print("✅ PASS")
else:
print("❌ FAIL")
print("-" * 40)
The next function is called writeReport. This function takes three parameters: 1) the results from analyzeData, 2) the name of the input file, and 3) the name of the output file. This function opens a new file and writes the report. The format of the report is shown in the examples below. writeReport creates a new file and does not return anything. Make sure you check the existence and contents of the newly created file to verify that your function works.
>>> results = analyzeData('watched1.txt')
>>> writeReport(results,'watched1.txt', 'report1.txt')
# The file report1.txt contains this text:
Report for watched1.txt
Total episodes watched: 6
Total viewing time: 185 minutes
Longest episode: 48 minutes
Unique shows viewed: 3
>>> results = analyzeData('watched2.txt')
>>> writeReport('watched2.txt', 'report2.txt')
# The file report2.txt contains this text:
Report for watched2.txt
Total episodes watched: 11
Total viewing time: 471 minutes
Longest episode: 55 minutes
Unique shows viewed: 5
>>> results = analyzeData('watched3.txt')
>>> writeReport('watched3.txt', 'report3.txt')
# The file report3.txt contains this text:
Report for watched3.txt
Total episodes watched: 92
Total viewing time: 3092 minutes
Longest episode: 61 minutes
Unique shows viewed: 17
>>> results = analyzeData('watched4.txt')
>>> writeReport('watched4.txt', 'report4.txt')
# The file report4.txt contains this text:
Report for watched4.txt
Total episodes watched: 50
Total viewing time: 2147 minutes
Longest episode: 89 minutes
Unique shows viewed: 50
The last function is called viewerAnalysis, and it pulls everything together. This function takes two parameters: the name of the input file and the name of the output file. This simple function calls analyzeData, and then sends the results from analyzeData to writeReport, along with the filename and desired report name. Nothing is returned or printed, but a new file is created. Make sure you check the existence and contents of the newly created file to verify that your function works.
Make sure that your file:
Starts with a comment indicating your name, the date, and any collaborators
Save your python file as username_tvshows.py, e.g, this is how Beyoncé Knowles would name her file: bk101_tvshows.py
Each function has a docstring
Each function is tested properly
Submit this python file in gradescope