Hey guys, I need assistance with my program need to write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 100. The application should let the user specify how many random numbers the file will hold. Once that program is done, need to write a program that reads from a file named ‘random.txt’ using a for loop with in file_object clause, display the numbers, and then display the following data: the sum total of the random numbers picked and the number of random numbers read from the file. I have both programs but the second program is not totalling the sum of the random numbers correctly.

Here are the two programs:

```````
# import the random module
import random

# define the main function
def main():

# open a file for writing and assign it to the variable random_numbers.
# this will reference the file object
random_numbers = open('ran_numbers.txt', 'w')

# get the number of random numbers to be generated
qty_numbers = int(input('Input how many random numbers should be written to the file:  '))

print('Your list of random numbers are: ')

# create a loop to generate the random numbers in the quantity specified
for count in range (qty_numbers):
number = random.randint(1,100)
# print the list of random numbers
print(number)

# convert the numbers to a string and write them to the file
random_numbers.write(str(number)+ '\n')

# close the file
random_numbers.close()

# tell the user that the numbers have been written to the file name.
print('Your list of random numbers have been written to the file named')
print('ran_numbers.txt')

# call the main function
main()

and the second:

def main():

random_numbers = open('ran_numbers.txt', 'r')

number = 0

total = 0

print("List of numbers:")
print(line)
total = total+int(line)
number +=1

print("The total of the numbers = "+str(total))

print("The number of the random numbers read from the file = "+str(number))

main()
``````

`
I am looking for assistance on where it might be having the issue, this is in the correct form witht he main methed being called at the end and def main() at the beginning. Like I said the only issue I am having is the second program is not totalling correctly.

## All 2 Replies

Problem lies in program one line 21 and 22 needs to be indented so that each random number gets written to the output file instead of just the last number(That's what you're codes currently doing.):

``````# import the random module
import random

# define the main function
def main():

# open a file for writing and assign it to the variable random_numbers.
# this will reference the file object
random_numbers = open('ran_numbers.txt', 'w')

# get the number of random numbers to be generated
qty_numbers =  6 #int(input('Input how many random numbers should be written to the file: '))
print('Your list of random numbers are: ')

# create a loop to generate the random numbers in the quantity specified
for count in range (qty_numbers):
number = random.randint(1,100)
# print the list of random numbers
print(number)

# convert the numbers to a string and write them to the file
random_numbers.write(str(number)+ '\n')

# close the file
random_numbers.close()

# tell the user that the numbers have been written to the file name.
print('Your list of random numbers have been written to the file named')
print('ran_numbers.txt')

# call the main function
main()
``````

Thank you for pointing that out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.