Random Number File Writer: Write a program that writes a series of random numbers to a file. Each random umber should be in range of 1 through 500. The application should let user specify how many numbers the file will hold.

Which I have.

__author__ = 'Preston Howard'

# Random Number File Writer: Program writes random numbers to a file. The numbers should range from 1 to 500.

import random

afile = open("numbers.txt", "w" )

for i in range(int(input('How many random numbers would you like?: '))):
    line = str(random.randint(1, 500))
    afile.write(line)
    print(line)

afile.close()

I'm needing help with the second part of the assignment.

Analyze Number File: Write a program that reads the file written in the program1. Display all numbers stored in the file, maximum number, minimum number, and average of all numbers

__author__ = 'Preston Howard'

#Analyze Number File: Reads a file and calculates the max, min, and average of the numbers in the file.

#Get the name of a file
filename = raw_input('Enter a file name: ')
total = 0
numberlength = 0
#Open the file
infile = open(filename, 'r')

#Read values from file and compute average
for line in infile:
    print line.rstrip("\n")
amount = float(line.rstrip("\n"))
total += amount
numberlength = numberlength + 1

average = total / numberlength

#Close the file
infile.close()

#Print the amount of numbers in file and average

print format(average, ',.2f')

Sorry I can't get the second prgroam to post in the code tags.

I think lines 15 to 17 should be indented to become part of the 'for' loop.

I just indented them but I get a wierd answer when I run the program to calc the average

I get

Enter a file name: numbers.txt
1183826028523
1,183,826,028,523.00

It is because your numbers are all written on one line in numbers.txt. In the first program, replace afile.write(line) with afile.write(line + '\n').

Be a part of the DaniWeb community

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