IT 116: Introduction to Scripting
Homework 8

Due

Sunday, April 6th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

random_numbers_write

	looping 10 times:
	   create a random number between 1 and 1000
	   convert this number to a string
	   write this number to the file adding a newline

count_odds

	set odd_count to 0
	for each line in the file:
	   convert the line into an integer
	   if the number is odd:
	      add 1 to odd_count
	return odd_count

largest_number

	set largest to 0
	for each line in the file:
	   convert the line into an integer
	   if this integer is greater than largest:
	      set largest to this number
	return largest

Test Code

Output

Suggestions


  1. At the top of the script file import the random module.
    Copy only the function headers for the four 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. In random_numbers_write replace the pass statement with a for loop than runs 10 times.
    Do this using a call to range with argument 10.
    Inside the for loop use an assignment statement to create the variable number.
    Give this variable a value by calling random.randint with arguments of 1 and 100.
    Print number.
    Copy the test code to the bottom of your script.
    Comment out all but the first 4 lines.
    Run the script.
    Fix any errors you find.
  3. Remove the print statement.
    In the step above, you created 10 random integers.
    You need to write those numbers to a file.
    But you need to turn those numbers into strings, because we are creating text files.
    Write an assignment statement that will turn number into a string.
    Print number.
    Run the script.
    Fix any errors you find.
  4. Remove the print in the for loop.
    Replace it with a statement that will write number followed by a linefeed character, \n, to the file. You need the linefeed character so each number is written to its own line.
    Run the script.
    Look at the file numbers.txt .
    It should contain the same numbers you saw in the previous step.
    Fix any errors you find.
  5. In count_odds remove the pass statement.
    Set odd_count to 0.
    Write a for loop that prints every line in file.
    Uncomment the next two lines in the test code.
    Run the script.
    Fix any errors you find.
  6. You need to convert each line in the file into a number.
    Remove the print statement.
    Replace it with a statement that creates the variable number by converting each line into an integer.
    Print number.
    Run the script.
    Fix any errors you find.
  7. Remove the print statement.
    Replace it with an if statement that will run if number is odd.
    If the remainder when you divide a number by 2 is 1, it is odd.
    Inside the if statement add 1 to odd_count.
    Print odd_count.
    Run the script.
    Fix any errors you find.
  8. Remove the print statement.
    Outside the for loop return odd_count.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  9. Remove the pass statement from largest_number. Replace it with a statement that sets largest to 0.
    Write a for loop that prints every line in the file.
    Uncomment the next two lines in the test code.
    Run the script.
    Fix any errors you find.
  10. Remove the print statement inside the for loop.
    Replace it with a statement that creates the variable number by converting each line into an integer.
    Print number.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement.
    Replace it with an if statement that runs if number is greater than largest. Inside the if statement, set largest to number.
    Print largest.
    Run the script.
    Fix any errors you find.
  12. Remove the print statement.
    Outside the for loop, return largest. 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 © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.