IT 116: Introduction to Scripting
Homework 11
Due
Thursday, October 30th at 11:59 PM
What You Need to Do
- Create the script hw11.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Make sure the script has a hashbang line and is executable
- Move it to an an hw11
directory on pe15.cs.umb.edu
Setup On Your Machine
- Open a text editor.
I would suggest the text editor built into the program IDLE
.
- Save the file as hw11.py
Specification
- This script creates a list of random integers
- Then finds the average, maximum and count of the even
numbers in this list
- The script must contain 4 functions
- random_number_list
- list_average
- list_maximum
- list_evens
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
- Header
def list_average(list):
- This function calculates the average value of the
numbers in the list specified by the parameter
list
- The average must be rounded to the nearest integer
- The function must return this average
- The function should use the following algorithm
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
- Header
def list_maximum(list):
- This function determine the the highest number
in the list specified by the parameter list
- The function must return this maximum value
- The function should use the following algorithm
set max to 0
for each number in the list:
if number is greater than max:
set max to number
return max
list_evens
- Header
def list_evens(list):
- This function counts the even numbers in a list
specified by the parameter list
- The function must return this count
- The function should use the following algorithm
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
- The output should look something like this
73
99
10
Suggestions
- Write this script in stages
- Test your script at each step
- Print the steps below
- And check them off as you finish each one
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
- Open IDLE
- Use the Open command in IDLE to open hw11.py
- Run your script inside IDLE
- Your output should look something like this
73
99
10
Copy the Script to Unix
- Open FileZilla and connect to
pe15.cs.umb.edu
- Go to your it116 directory
- Go to your hw directory
- Right-click in the whitespace inside the
hw directory
- Enter hw11 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
- Right-click on the file and select "Permissions" from
the menu
- Enter 755 in the box provided
- This will make the script executable
Testing the Script on Unix (Optional)
- Connect to pe15.cs.umb.edu
using an ssh client like putty.exe (Windows)
or ssh (Mac)
- Go to the directory for this exercise
cd it116/hw/hw11
- Run this script
python3 hw11.py
- You should see something like this
73
99
10
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.