IT 116: Introduction to Scripting
Homework 8
Due
Sunday, November 2nd at 11:59 PM
What You Need to Do
- Create the script hw8.py
- Make sure it obeys the rules in
Rules for Homework Scripts
- Move it to an an hw8
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 hw8.py
Specification
- This script will use functions in the
random module create, read
and work with files of integers
- The script must contain 3 functions
- random_div_3
- random_file_create
- count_evens
Functions
random_div_3
- Header
def random_div_3():
- This function returns a random integer from
1 to 100 that is divisible by 3
- The function should use the following algorithm
while True:
get a random number
if the random number is divisible by 3:
return the number
random_file_create
count_evens
- Header
def count_evens(file):
- This function returns the number of even integers
in a file
- The function should use the following algorithm
set even_count to 0
for each line:
convert line to number
if the number is even:
increment even_count
return even_count
Test Code
Output
- The output should look something like this
4
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
-
Copy only the function headers for the five 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_file_create.
Replace it with a for
loop that loops 10 times.
Inside the for
loop print something.
Copy the test code to the bottom of your script.
Comment out all lines in the test script except the first four.
Run the script.
Fix any errors you find.
-
Remove the
print
statement from
random_file_create.
Replace it with an assignment statement that give the variable
number the value returned by calling
random_div_3().
random_div_3 takes no argument.
Print number.
Remove the pass
statement from
random_div_3.
In its place write an assignment statement that gives the variable
number a randomly chosen value between
1 and 100.
You can use random.randint()
to create this value.
Return number.
Run the script.
Fix any errors you find.
-
Now you need to change the code so that
random_div_3 only gives a value that
is divisible by 3.
You will need a while
loop to do this.
Remove the return
statement.
Above the assignment statement for
number write
while True:
This loop would normally run forever, but the code will break
out of the loop using a return
statement.
Under the assignment statement write an if
statement
that will work if number is divisible by
3.
You will need to use the remainder operator
%.
A number is divisible by 3 if, when you divide the number by 3,
the remainder is 0.
Inside the if
statement return
number.
Run the script.
Fix any errors you find.
-
Now you are need to change random_file_create
so it writes number to the file pointed
to by the file parameter.
This is a little bit complicated.
Instead of printing, you will have to make a call to
file.write
.
Since we need to create a text file, we will have to convert
number to a string.
Since each number must appear on a separate line, we must
concatenate the linefeed character to the string version of
number.
First remove the print
statement in
random_file_create.
Replace it with a call to file.write
.
Inside the parentheses of the argument, convert
number to a string and concatenate it
with \n.
Run the script.
To see if it worked look at the file
Fix any errors you find.
-
Remove the
pass
statement from
count_evens.
Give the variable even_count the value 0.
Loop through file with a for
loop.
Inside the loop print each line.
Uncomment the last line in the test script.
Run the script.
You should see numbers with "None" on the last line.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an assignment statement that creates the variable
number by converting each line into an
integer.
Print number.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an if
statement that will work if
number is even.
You can do this using the remainder operator,
%.
If a number is even, its remainder when divided by 2 is 0.
Inside the if
statement increment
even_count by 1.
Outside the for
loop, return
even_count.
Run the script.
Fix any errors you find.
Testing on Your Machine
- Open IDLE
- Use the Open command in IDLE to open hw8.py
- Run your script inside IDLE
- Your output should look something like this
4
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 hw8 in the dialog box
- Click and drag your script from the bottom left panel
to the bottom right panel
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/hw8
- Run this script
python3 hw8.py
- You should see something like this
4
- The text in blue is what you
enter at the command line
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.