Nested Loops

In [6]:
from random import sample
def make_nums_multi(nums):
    outfile = open("data_m.txt", 'w')
    count = 0
    for n in sample(range(2000), k=nums):            
        outfile.write(str(n))
        count += 1
        if count % 5 == 0:
            outfile.write("\n")
        else:
             outfile.write(",")
    outfile.close()
In [10]:
def average7():
    filename = input("Filename of numbers: ")
    infile = open(filename, 'r')
    _sum = 0.0
    count = 0
    
    line = infile.readline()
    while line != "":
        # Deal with multiple numbers per line here!
        for _str in line.split(","):
            _sum = _sum + float(_str)
            count = count + 1
        line = infile.readline()
        
    print("\nThe average of the numbers is", _sum/count)
    infile.close()
In [8]:
make_nums_multi(100)
In [13]:
average7()
Filename of numbers: data_m.txt

The average of the numbers is 1056.75
In [ ]: