IT 116: Introduction to Scripting
Homework 8

Due

Sunday, November 2nd at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

random_div_3

	while True:
	  get a random number
	  if the random number is divisible by 3:
	     return the number

random_file_create


count_evens

	set even_count to 0
	for each line:
	   convert line to number
	   if the number is even:
	      increment even_count
	return even_count

Test Code

Output

Suggestions

  1. Copy only the function headers for the five functions listed above into your script file.
    Under each header, write pass.
    Make sure this statement is indented.
    Run the script.
    Fix any errors you find.
  2. Remove the pass statement from random_file_create.
    Replace it with a for loop that loops 10 times.
    Inside the for loop print something.
    Copy the test code to the bottom of your script.
    Comment out all lines in the test script except the first four.
    Run the script.
    Fix any errors you find.
  3. Remove the print statement from random_file_create.
    Replace it with an assignment statement that give the variable number the value returned by calling random_div_3().
    random_div_3 takes no argument.
    Print number.
    Remove the pass statement from random_div_3.
    In its place write an assignment statement that gives the variable number a randomly chosen value between 1 and 100.
    You can use random.randint() to create this value.
    Return number.
    Run the script.
    Fix any errors you find.
  4. Now you need to change the code so that random_div_3 only gives a value that is divisible by 3.
    You will need a while loop to do this.
    Remove the return statement.
    Above the assignment statement for number write
    while True:
    This loop would normally run forever, but the code will break out of the loop using a return statement.
    Under the assignment statement write an if statement that will work if number is divisible by 3.
    You will need to use the remainder operator %.
    A number is divisible by 3 if, when you divide the number by 3, the remainder is 0.
    Inside the if statement return number.
    Run the script.
    Fix any errors you find.
  5. Now you are need to change random_file_create so it writes number to the file pointed to by the file parameter.
    This is a little bit complicated.
    Instead of printing, you will have to make a call to file.write.
    Since we need to create a text file, we will have to convert number to a string. Since each number must appear on a separate line, we must concatenate the linefeed character to the string version of number.
    First remove the print statement in random_file_create.
    Replace it with a call to file.write.
    Inside the parentheses of the argument, convert number to a string and concatenate it with \n. Run the script.
    To see if it worked look at the file Fix any errors you find.
  6. Remove the pass statement from count_evens.
    Give the variable even_count the value 0.
    Loop through file with a for loop.
    Inside the loop print each line.
    Uncomment the last line in the test script.
    Run the script.
    You should see numbers with "None" on the last line.
    Fix any errors you find.
  7. Remove the print statement.
    Replace it with an assignment statement that creates the variable number by converting each line into an integer.
    Print number.
    Run the script.
    Fix any errors you find.
  8. Remove the print statement.
    Replace it with an if statement that will work if number is even.
    You can do this using the remainder operator, %.
    If a number is even, its remainder when divided by 2 is 0.
    Inside the if statement increment even_count by 1.
    Outside the for loop, return even_count.
    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.