Question
Suppose we have a file that contains several lines of comma-separated numbers. Write a python function score_averages(inputf, outputf) that reads the input file, calculates the
Suppose we have a file that contains several lines of comma-separated numbers. Write a python function score_averages(inputf, outputf) that reads the input file, calculates the average of each line of scores, and writes those averages to the output file. Note that the variables input and output are strings that refer to the names of the files.
Important notes: the last line should have a newline
When opening files, you must use the complete file path. For example, if you wanted to access a file called abc.txt, you would need to use the path "file_path/abc.txt".
scores.txt
"""
0,50,100 85,95,90
"""
scores1.text
"""
0,0 100,100,100,100,100 25,50,75
"""
Test Cases
def test_get_averages():
# Note: you can open the result files and visually check the contents!
# test 1
get_averages("scores.txt", "averages.txt")
f = open("averages.txt", "r")
result = f.readlines()
assert(result == ["50.0 ", "90.0"]) # no newline after 90.0
f.close()
# test 2
get_averages("scores1.txt", "averages1.txt")
f = open("averages1.txt", "r")
result = f.readlines()
assert(result == ["0.0 ", "100.0 ", "50.0"]) # no newline after 50.0
f.close()
print("done!")
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