SD 212 Spring 2023 / Homeworks


This is the archived website of SD 212 from the Spring 2023 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

hw14: Average rating

  • Due before the beginning of class on Friday, February 10

The data

Your program will read from a single text file which has movie ratings (1-10 scale) on each line. Some lines don’t have any numerical value, and those should be treated like a neutral rating of 5.

For example if these are the lines of some file:

7
9
meh
whatever
10
it was okay
8
1

Then that would count as ratings of 7, 9, 5, 5, 10, 5, 8, and 1, which averages to 6.25.

We give you two sample files avatar.txt and smoochy.txt

Your task

Write a Python program ratings.py that asks for a filename, then tries to read in the ratings from that file and print the average. Each line of the file has one rating, and any non-numerical line should be treated like a rating of 5.

If the file doesn’t exist, your program should print out an error message no such file and exit immediately.

Sample runs

Here are a few sample runs assuming your directory contains avatar.txt and smoochy.txt.

roche@ubuntu$ python3 ratings.py
filename: toystory.txt
no such file
roche@ubuntu$ python3 ratings.py
filename: avatar.txt
average: 6.25
roche@ubuntu$ python3 ratings.py
filename: smoochy.txt
average: 6.35

Submit command

To submit files for this homework, run one of these commands:

submit -c=sd212 -p=hw14 ratings.py
club -csd212 -phw14 ratings.py