IT 116: Introduction to Scripting
Homework 8

Due

Sunday, November 3rd at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

random_file_create

	loop entries times:
	   create a random number between min and max
	   turn this number into a string
	   add a linefeed at the end of this string
	   write this string to file

minimum_number

	set min to 1000
	for each line:
	   convert the line into an integer a assign it to num
	   if num less than min:
	      set min to num
	return min

average_numbers

	set total to 0
	set count to 0
	for each line in the file:
	   turn the line into an integer
	   add this integer to total
	   increase count by 1
	set average to total divided by count
	return the rounded average

Test Code

Output

Suggestions


  1. Create the file hw8.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.
    Fix any errors you find.
  2. Remove the pass statement from random_file_create.
    Replace it with for loop that runs as many times as the number in entries.
    Inside the for loop, print the loop variable.
    Copy the test code to the bottom of your script.
    Comment out all but the first 4 lines of the test code beginning by placing a # at the beginning of each line.
    Run the script and fix any errors you find.Run the script.
    Fix any errors you find.
  3. Remove the print statement.
    Replace this statement with one that creates a random integer between min and max and assigns it to the variable rand_num.
    Convert rand_num to a string using an assignment statement.
    You have to do this in order to write the value to a text file.
    Print rand_num.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement.
    Replace it with a statement that writes rand_num, followed by a newline (\n), to file. Run the script.
    Look at the file numbers.txt to make sure it worked correctly.
    Fix any errors you find.
  5. Remove the pass statement in minimum_number.
    Set min to 100. Write a for loop over file.
    Inside the for loop print each line.
    If you don't do this, a blank line after each number.
    Uncomment the next two lines in the test code.
    Run the script.
    Fix any errors you find.
  6. Remove the print statement.
    Turn each line into an integer and assign it to num.
    Print num.
    Run the script.
    Fix any errors you find.
  7. Remove the print statement.
    Replace it with an if statement that will run if num is less than min.
    The body of the if statement should set min to num and then print min.
    Run the script.
    Fix any errors you find.
  8. Remove the print statement.
    Outside the loop return min.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  9. Remove the pass statement from average_numbers.
    Replace it with a statement that sets the variable total to 0.
    Set the variable count to 0.
    Print total and count.
    Uncomment the next 2 lines in the test code. Run the script.
    Fix any errors you find.
  10. Remove the print statement.
    Write a for loop that runs through the lines in the file.
    Inside the loop, use an assignment statement to create the variable num by calling int on the line.
    Print num.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement.
    Replace it with a statement that adds num to total. Outside the loop, print total.
    Run the script.
    The total should be 1329.
    Fix any errors you find.
  12. Remove the print statement.
    Inside the for loop write a statement that adds 1 to count.
    Outside the loop, print count.
    Run the script.
    The count should be 20.
    Fix any errors you find.
  13. Remove the print statement.
    Write an assignment statement that sets the variable average to total divided by count.
    Print average.
    Run the script.
    The average should be 66.45.
    Fix any errors you find.
  14. Remove the print statement.
    Replace it with a return statement that returns the result of running round on average.
    Uncomment the last line in the test code.
    Run the script.
    The average should be 66.
    Fix any errors you find.

Testing on Your Machine

Copy the Script to Unix

Testing the Script on Unix (Optional)

Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.