Feb-27-2018, 01:24 AM
This is the assignment: Write a program that read numbers from a text file named "data.txt" and store the average in a second file named "average.txt". The numbers are line separated (each line in the file contains exactly one number.) You will have to create the file data.txt for testing purpose.
I will post what I have thus far. I believe I am way over-thinking it, but I've been trying to figure this out for a couple of hours now. It is giving me the error that num_list is not defined. I had it "defined" before, but it was only returning the first number in the data.txt file.
The data.txt file consists of these numbers:
379
99
7
380
907
567
988
610
223
865
192
138
503
817
585
I will post what I have thus far. I believe I am way over-thinking it, but I've been trying to figure this out for a couple of hours now. It is giving me the error that num_list is not defined. I had it "defined" before, but it was only returning the first number in the data.txt file.
The data.txt file consists of these numbers:
379
99
7
380
907
567
988
610
223
865
192
138
503
817
585
import math def load_numbers(filename): num_list = [] with open('data.txt', 'r') as f: for line in f: num = line.strip() num_list += [num] return (num_list) def main(): load_numbers('data.txt') print(num_list) main()