IT 116: Introduction to Scripting
Homework 8

Due

Sunday, March 31st at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

random_integers_write

	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

average_from_file

	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

divide_by_three_count

	set three_count to 0
	for each line in the file:
	   convert the line into an integer
	   if the number is evenly divisible by 3:
	      add 1 to odd_count
	return odd_count

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_integers_write.
    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 from average_from_file.
    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.
  6. 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.
  7. 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 1518.
    Fix any errors you find.
  8. 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 30.
    Fix any errors you find.
  9. 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 50.6.
    Fix any errors you find.
  10. Remove the print statement.
    Replace it with a return statement that returns the result of running round on average.
    Uncomment the next line in the test code.
    Run the script.
    The average should be 51.
    Fix any errors you find.
  11. Remove the pass statement from divide_by_three_count.
    Replace it with a statement that sets three_count to 0.
    Write a for loop that iterates through file.
    Inside the loop set num to the value of the line when run through the int() conversion function.
    Print num.
    Uncomment the next two lines in the test code.
    Run the script.
    Fix any errors you find.
  12. Remove the print statement.
    Replace it with an if statement that will run if num is evenly divisible by 3.
    A number is evenly divisible by 3 if the remainder, when divided by 3 is 0.
    Inside the if statement add 1 to three_count.
    Outside the for loop print three_count.
    Run the script.
    Fix any errors you find.
  13. Remove the print statement.
    Replace it with a statement that returns three_count.
    Uncomment the last line in the test code.
    Run the script.
    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.