IT 116: Introduction to Scripting
Homework 11

Due

Sunday, April 21st at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

div_three_list_create

	create an empty list
	for i running from 0 to length:
	   create a random integer using random.randint
	   if the number is evenly divisible by three:
	      append it to the list
	return the list

odds_count

	set the variable odd_count to 0
	for each number in the list:
	   if the number is odd:
	      increment odd_count
	return odd_count
	

triple_list

	for each number in the list:
	   multiply the value of the element by three
	

max_min_list

	sort the list
	return the last and first number in the list
	

Test Code

Output

Suggestions

  1. Create a hashbang line on the first line of the script.
    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. Import the random module.
    Remove the pass statement from div_three_list_create.
    Create an empty list.
    Write a while loops that keeps running as long as the length of the list is less than length.
    Inside the loop assign the variable num a random integer by calling randint.
    Print num.
    Append num to the list.
    Copy all of the test code to the bottom of your script.
    Comment out all but the first two lines of the test code.
    Do this by inserting # at the beginning of the line.
    Run the script.
    Fix any errors you find.
  3. Remove the print and append statements.
    Replace them with an if statement that runs if num is evenly divisible by 3.
    You will need to use the % operator.
    Inside the loop print num.
    Then append num to the list.
    Run the script.
    Fix any errors you find.
  4. Remove the print statement.
    Outside the for loop, return the list.
    Uncomment the next two lines in the test code.
    Run the script.
    Fix any errors you find.
  5. Remove the pass statement from odds_count.
    Replace it with a statement that creates the variable odd-count and sets its value to 0.
    Write a for loop over list.
    Inside the loop print each number.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  6. Remove the print statement.
    Replace it with an if statement that runs if num is odd.
    You will need to use the % operator.
    Inside the if statement print num.
    Run the script.
    Fix any errors you find.
  7. Remove the print statement.
    Replace it with a statement that increments odds_count.
    Outside the for loop return odds_count.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  8. Remove the pass statement from triple_list.
    Write a for loop that creates indexes for each entry in the loop.
    Inside the loop use the index to print the value of each number.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  9. Remove the print statement.
    Use the index and the augmented assignment operator *= to change the value of the entry by multiplying it by 3.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  10. Remove the pass statement from max_min_list.
    Replace it with a statement that sorts the list.
    Print the list.
    Uncomment the next line in the test code.
    Run the script.
    Fix any errors you find.
  11. Remove the print statement.
    Replace it with a statement that returns the last and first number in the list.
    Uncomment the last two lines 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.