Lab 11 (Bonus Assignment) Read numbers from file Objectives: Practice basic file
ID: 3676781 • Letter: L
Question
Lab 11 (Bonus Assignment) Read numbers from file Objectives: Practice basic file input and string processing. Write a function to read out numbers stored in a text file. The file contains only one line that stores multiple numbers. Each number is separated by one or more commas. The first and the second numbers are separated by one comma. The second and the third numbers are separated by two commas. The third and the fourth numbers are separated by three commas. And so on. No space is allowed before or after any comma. There is no comma after the last number; in other words, the line will not end with a comma. For the following example, eight numbers are stored in that line. 51,2,,2,,,49,,,,15,,,,,2,,,,,,1,,,,,,,14 The function should be named decipher and be able to accept a string, representing a file name, as argument. It should be able to read all numbers stored in the designated file, and return their average. A sample file, named test.txt, is provided for your unit testing. If your implementation is correct, decipher("test.txt") should return 17.0, because the average of the numbers stored in the provided sample file test.txt is 17.0. Your decipher function should be able to take any file name as an argument and read all numbers stored in that designated file. Function specifications: Function name decipher Input parameter a string that is a file name return value a number that is the average of all numbers stored in the designated file Testing: Develop at least 2 test cases, calculate the correct results by hand, and then confirm that your program gives the same results. To test the correctness of your function, you can make function calls, one for each of your test cases. For example, using the file I provided to you, print(decipher("test.txt")) should display 17.0 on screen. When you are ready to submit, remove all such function calls from your script file. You do not need to submit your test cases. Code specifications: Your submitted .py file should contain ONLY your function definition for decipher. DO NOT include main function or your testing codes. This is what inside the text.txt 51,2,,2,,,49,,,,15,,,,,2,,,,,,1,,,,,,,14
Explanation / Answer
def function_name(filename="test.txt"):
text_file = open (filename, "r")
for line in f.readlines():
# Strip the line to remove whitespace if any
line = line.strip()
# Split the line on the basis of the comma
values = line.split(",")
# Loop over strings and convert them to integers.
# ... Then sum them.
total = 0
average=0
a-len(values) # This will calculate the length of the values.
for value in values:
total += int(value)
average= total/a-len
return average
In this program we have taken all the possible values which are used to calculate the average of the numbers present in the list.
Here the length of the list also has to be calculated separately because we need the total count of the numbers in the list.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.