IT 116: Introduction to Scripting
Homework 11

Due

Thursday, October 30th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

random_number_list

	create an empty list
	loop for the number of times specified by length
	   create a random integer with a value between 
	   max and min
	   append this integer to the list
	return the list

list_average

	set total to 0
	for each number in the list:
	   add it to total
	calculate the average by dividing total by the length of the list
	return the rounded average

list_maximum

	set max to 0
	for each number in the list:
	   if number is greater than max:
	      set max to number
	return max

list_evens

	set even_count to 0
	for each number in the list:
	   if the number is even:
	      increase even_count by 1
	return even_count

Test Code

Output

Suggestions


  1. Create a hashbang line on the first line of the script.
    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. Remove the pass statement from random_number_list.
    Replace it with a statement that creates an empty list.
    Write a for loop that will run the number of times specified by length.
    Inside the loop, print the value of the loop variable.
    Copy the test code to the bottom of the script.
    Comment out all but the first two lines of the test script.
    Run the script.
    Fix any errors you find.
  3. Remove the print statement.
    Replace it with an assignment statement that give num a value created by a call to randint in the random module.
    You will need to use min and max as arguments to randint.
    Print num.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement.
    Replace it with a statement that appends num to list.
    Outside the loop print list.
    Run the script.
    Fix any errors you find.
  5. Remove the print statement.
    Replace it with a statement that returns list.
    Remove the pass statement from list_average.
    Replace it with a statement that prints list.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  6. Remove the print statement.
    Replace it with a line that sets total to 0.
    Write a for loop over list using num as the loop variable.
    Inside the loop 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.
    Fix any errors you find.
  8. Remove the print statement.
    Replace it with a statement that calculates average by dividing total by the length of list. Print average.
    Run the script.
    Fix any errors you find.
  9. Remove the print statement.
    Replace it with a statement that returns the rounded value of average.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  10. Remove the pass statement from list_maximum.
    Replace it with a statement that sets max to 0.
    Write a for loop over list that uses num as the loop variable.
    Inside the for loop print num.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement.
    Replace it with an if statement that will run if num is greater than max.
    Inside the if statement set max to num.
    Print max.
    Run the script.
    Fix any errors you find.
  12. Remove the print statement.
    Outside the for loop return max.
    Uncomment the next line in the test code. Run the script.
    Fix any errors you find.
  13. Remove the pass statement from list_evens.
    Set even_count to 0. Write a for loop over list that uses num as the loop variable.
    Inside the for loop print num.
    Uncomment the next line in the test code. Run the script.
    Fix any errors you find.
  14. Remove the print statement.
    In its place write an if statement that will run if num is even.
    A number is even if its remainder when divided by 2 is 0.
    Inside the if statement print num.
    Run the script.
    Fix any errors you find.
  15. Remove the print statement.
    Replace it with a statement that adds 1 to even_count.
    Outside the for loop return even_count.
    Uncomment the last line of 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.