IT 116: Introduction to Scripting
Homework 11

Due

Sunday, April 26th at 11:59 PM

What You Need to Do

Setup On Your Machine

Specification

Functions

div_three_list_create

	create the empty list list
	for i running from 0 to length of list:
	   create a random integer num using random.randint()
	   if num is evenly divisible by three:
	      append num to list
	return list

odds_count

   set odd_count to 0
   for each number in list:
      if number is odd:
         add 1 to  odd_count
   return odd_count
	

triple_list

   for index runing from 0 to the length of list:
      set the variable number to the entry in list with position index
      give number a new value by multiplying it by 3
      use this new value of number to replace the entry in list specified by index
	

max_min_list

   sort list
   set first to the first value in list using an index
   set last to the last value in list using an index
   return first and last
	

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.