-1

I am trying to write two functions. One generates random numbers between 1 and 100 with the parameter being the frequency of numbers which saves the information to a file, and another one reads this file and displays the: total sum, average, highest and lowest value. I have managed to get the first function to work however the secound function does not. When running the program I get the TypeError: 'int' object is not iterable due to the "number=int(number)" line in the readNumbers() function, which I don't understand because I thought the number had to be changed from a string to an int in order for equations to work. Any help would be appreciated. Also is there a method of finding the maximum and minimum values without using max() and min(), I would personally like to know.

def generateNumbers(i):
    myFile=open("numbers.txt","w")

    for n in range(i):
        import random
        numbers=random.randint(1,100)
        numbers=str(numbers)
        myFile.write(numbers)    
     myFile.close()

generateNumbers(400)

def readNumbers():
    myFile=open("numbers.txt","r")

    for number in myFile:
        number=int(number)
        total=(sum(number))
        average=sum/float(len(number))    

    while number!=-1:
         num_list.append(number)
         high= max(num_list)
         low= min(num_list)

     print(total,average,high,low)

     myFile.close()

readNumbers()

2 Answers2

0

Your program has several problems.

Writing the numbers:

You're not separating the numbers in anyway, so your file will contain a very long string of digits (and to nitpick, your numbers is actually random_number)

if you f.ex myFile.write("\n") after myFile.write(random_number) each number will be separated by a newline.

Now, since separating the numbers with newline, your reading will work, except that you should read them into an array, then do total and average.

ie:

num_list = []
for number in myFile:
    if number != "\n": # there is an empty line at the end
        num_list.append(int(number))

total = sum(num_list)
average = total / len(num_list)
high = max(num_list)
low = min(num_list)

Note that you don't need the while loop

You could also do the reading with python's list comprehension, and close the file automatically with a context manager

with open("numbers.txt", "r") as f:
    num_list = [int(number) for number in f if number != "\n"]

(edited to avoid error on empty line at end)

ttyridal
  • 458
  • 4
  • 15
0

Kindal is right.

You should do something like:

sum = 0;
count = 0;
for number in myFile:
    sum += number
    count += 1
average = sum /count

Moreover, I think that you can optimise a bit your code. Read the file and create a list, then you can easily calculate sum, avg, min, max.

with open("my filename.txt", 'r') as myFile:
   number_list = list()
   for number in myFile:
       number = int(number)
       if number != -1:  #Is it necessary for you?
           number_list.append(number)
sum = sum(number_list)
average = sum / len(number_list)
min_val = min(number_list)
max_val = max(number_list)