Python 1: Introduction to Python
Homework 7

Due

Sunday

Deliverables

There is one deliverable for this assignment

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 hw7.py.
    Import the random module.
    Create the header for each of the functions.
    Each function needs a body or you will get an error when you run it.
    For each of these functions write the special Python statement pass.
    This statement does nothing but it will not cause an error when run.
    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 filename.
    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.
  3. Add a for loop to random_number_file_create that will run for 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.
  4. 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, and run cat on the file numbers.txt.
    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 filename.
    Use a for 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 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.
  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 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 code 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.

Output

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