Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 def random_number_file_create(min, max, filename, entries): This

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

 def random_number_file_create(min, max, filename, entries):

This function must create a file where each line has a pseudorandom number. Each of these numbers must be integers 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

 def lines_print(filename):

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

 def lines_count(filename):

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

 def total_numbers_in_file(filename):

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.

Suggestions:

Create the file hw8.py. Import the random module. Create the header for each of the functions. For the body of each of the functions, use pass. Run the script and fix any errors you find.

Remove the pass statement from random_number_file_create. Replace it with a statement that creates a file object for writing for the filename given by the parameter. Add the following function call to the bottom of the file

random_number_file_create(50, 100, 'numbers.txt', 20)

Run the script and fix any errors you find.

Add a for loop to random_number_file_create that will run for as the number of times specified by the parameter entries. Inside the loop print the loop variable. Run the script and fix any errors you find.

Inside the for loop, remove the statement that prints the loop variable. Replace this statement with one that creates a random integer between min and max and assigns it to a variable. Add a statement to print this variable. Run the script and fix any errors you find.

Inside the for loop, replace the statement that prints the loop variable with one that writes this value to the file. Remember to turn the value into a string and to follow it with the linefeed character . Run the script, and run cat on the file numbers.txt. Fix any errors you find.

Replace the pass statement in lines_print with a statement that creates a file object for reading on the filename given by the parameter filename. Use a loop to print the lines in the file. Remove the call to random_number_file_create that you entered earlier. Add the first 5 lines of the test code to the bottom of the file. Run the script and fix any errors you find.

Replace the pass statement in lines_count with a statement that creates a file object for reading on the filename given by the parameter filename. Set a accumulator variable to 0. Write a loop that loops through the lines in the file. Do not print the lines but increment the accumulator for each line in the file. Return the accumulator value. Add the 6th and 7th lines in the test code to the bottom of the file. Run the script and fix any errors you find.

Replace the pass statement in total_numbers_in_file with a statement that creates a file object for reading on the filename given by the parameter filename. Set a accumulator variable to 0. Write a loop that loops through the lines in the file. Inside the loop, convert each line into an integer and add it to the accumulator. Return the accumulator value. Add the remainder of the test code. Run the script and fix any errors you find.

Test Code

At the bottom of the script, you must have the following code

FILENAME = "numbers.txt" random.seed(83) random_number_file_create(50, 100, FILENAME, 20) lines_print(FILENAME) print() entries = lines_count(FILENAME) print("Entries:", entries) total = total_numbers_in_file(FILENAME) average = round(total/entries) print("Total:", total) print("Average:", average)

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

81 79 55 58 74 52 53 91 63 62 61 69 55 53 76 59 72 69 85 62 Entries: 20 Total: 1329 Average: 66

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions