IT 116: Introduction to Scripting
Homework 8
Due
Sunday, March 31st 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_integers_write
- average_from_file
- divide_by_three_count
Functions
random_integers_write
loop entries times:
create a random number between min and max
turn this number into a string
add a linefeed at the end of this string
write this string to file
average_from_file
set total to 0
set count to 0
for each line in the file:
turn the line into an integer
add this integer to total
increase count by 1
set average to total divided by count
return the rounded average
divide_by_three_count
set three_count to 0
for each line in the file:
convert the line into an integer
if the number is evenly divisible by 3:
add 1 to odd_count
return odd_count
Test Code
- Your script must contain the following statements
random.seed(65)
FILENAME = "numbers.txt"
file = open(FILENAME, "w")
random_integers_write(file, 1, 100, 30)
file = open(FILENAME, "r")
average = average_from_file(file)
print(average)
file = open(FILENAME, "r")
three_count = divide_by_three_count(file)
print(three_count)
- They should appear at the bottom of your script
Output
- The output should look something like this
51
12
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 the file hw8.py.
Import the random module.
Create the header for each of the functions.
Each function needs a body or you will get an error when you run it.
For each of these functions write the special Python statement pass
.
This statement does nothing but it will not cause an error when run.
Run the script.
Fix any errors you find.
-
Remove the
pass
statement from
random_integers_write.
Replace it with for
loop that runs as many times as
the number in entries.
Inside the for
loop, print the loop variable.
Copy the test code to the bottom of your script.
Comment out all but the first 4 lines of the test code beginning
by placing a # at the beginning of each
line.
Run the script and fix any errors you find.Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace this statement with one that creates a random integer between
min and max and
assigns it to the variable rand_num.
Convert rand_num to a string using an
assignment statement.
You have to do this in order to write the value to a text file.
Print rand_num.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that writes
rand_num, followed by a newline
(\n), to file.
Run the script.
Look at the file numbers.txt to make sure
it worked correctly.
Fix any errors you find.
-
Remove the
pass
statement from
average_from_file.
Replace it with a statement that sets the variable
total to 0.
Set the variable count to 0.
Print total and
count.
Uncomment the next 2 lines in the test code.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Write a for
loop that runs through the lines in the file.
Inside the loop, use an assignment statement to create the variable
num by calling int
on the
line.
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.
The total should be 1518.
Fix any errors you find.
-
Remove the
print
statement.
Inside the for loop
write a statement that adds 1
to count.
Outside the loop, print count.
Run the script.
The count should be 30.
Fix any errors you find.
-
Remove the
print
statement.
Write an assignment statement that sets the variable
average to total
divided by count.
Print average.
Run the script.
The average should be 50.6.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a return
statement that returns
the result of running round
on average.
Uncomment the next line in the test code.
Run the script.
The average should be 51.
Fix any errors you find.
-
Remove the
pass
statement from
divide_by_three_count.
Replace it with a statement that sets
three_count to 0.
Write a for
loop that iterates through
file.
Inside the loop set num to the value
of the line when run through the int()
conversion
function.
Print num.
Uncomment the next two lines 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 evenly divisible by 3.
A number is evenly divisible by 3 if the remainder, when divided
by 3 is 0.
Inside the if
statement add 1 to
three_count.
Outside the for
loop print
three_count.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that returns
three_count.
Uncomment the last line in 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 hw8.py
- Run your script inside IDLE
- Your output should look something like this
51
12
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
- 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/hw8
- Run this script
python3 hw8.py
- You should see something like this
51
12
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.