Specification The script must define 4 functions random_number_file_create lines
ID: 3685123 • Letter: S
Question
Specification
The script must define 4 functions
random_number_file_create
lines_print
lines_count
total_numbers_in_file
random_number_file_create
This functions must have the following header
This function must create a file where each line has a pseudorandom number.
Each of these numbers must between the values of the parameters min and max.
The name of the file is given by the parameter filename.
The number of lines in the file is given by the parameter entries.
The function must close the file after it has written all the values.
lines_print
This functions must have the following header
This function must print each line of the file.
There should be no blank lines between the lines of the file.
The function must close the file after it has printed all the lines.
lines_count
This functions must have the following header
This function must count the number of lines in the file and return that total.
The function must close the file after it has read all the lines.
total_numbers_in_file
This functions must have the following header
This function must calculate the total of all the numbers in the file and return that total.
The function must close the file after it has read all the lines.
Run code
At the bottom of the script you must have the following code
The code above will run the functions you create, and make it easier for me to score this assignment.
Testing
When you run your script the output should look exactly like this
Explanation / Answer
import random
def random_number_file_create(min, max, filename, entries):
randfile = open(filename, "w" )
for i in range(entries):
line = str(random.randint(min, max)) + " "
randfile.write(line)
randfile.close()
def lines_print(filename):
f = open(filename, "r")
for line in f.readlines():
print line
f.close()
def lines_count(filename):
f = open(filename, "r")
count = 0
for line in f.readlines():
count = count + 1
return count
f.close()
def total_numbers_in_file(filename):
distances = []
with open(filename, 'r') as f:
for line in f:
if line[0].isdigit():
distances.extend(line.splitlines())
L = [int(n) for n in distances if n]
total = sum (L)
return total
FILENAME = "numbers.txt"
random.seed(83)
random_number_file_create(50, 100, FILENAME, 20)
lines_print(FILENAME)
print()
entries = lines_count(FILENAME)
total = total_numbers_in_file(FILENAME)
average = round(total/entries)
print("Entries:", entries)
print("Total:", total)
print("Average:", average)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.