IT 116: Introduction to Scripting
Homework 8

Due Sunday, November 5th at 11:59 PM

Deliverables

There is one deliverable for this assignment It must be in an hw8 directory, which you must create inside a hw directory inside you it116 directory.

Make sure the script obeys all the rules in the Script Requirements page.

Copy the file numbers.txt from /home/ghoffman/course_files/it116_files to your hw8 directory.

Specification

The script must define 4 functions

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

  1. 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.
  2. 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 a statement to the bottom of the file that calls random_number_file_create.
    Run the script and fix any errors you find.
  3. Add a for loop to random_number_file_create that will run for as many times as there are entries that need to be created.
    Inside the loop print the loop variable.
    Run the script and fix any errors you find.
  4. Add an import statement for the random functions.
    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.
  5. 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 \n. Run the script, look at the output file and fix any errors you find.
  6. Replace the pass statement in lines_print with a statement that creates a file object for reading on the filename given by the parameter.
    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.
  7. Replace the pass statement in lines_count with a statement that creates a file object for reading on the filename given by the parameter.
    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 line in the test code to the bottom of the file.
    Run the script and fix any errors you find.
  8. 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.
    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)
total   = total_numbers_in_file(FILENAME)
average = round(total/entries)
print("Entries:", 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
Be sure to run this script on the Unix machine so you know it works in the environment in which I will run it when I score your homework.