IT 116: Introduction to Scripting
Homework 8
Due
Sunday, November 3rd 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 create a text file of random integers
- Then calculate the minimum and average of the integers
in the file
- This script must contain three functions
- random_number_file_create
- minimum_number
- average_numbers
Functions
random_file_create
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
minimum_number
set min to 1000
for each line:
convert the line into an integer a assign it to num
if num less than min:
set min to num
return min
average_numbers
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
Test Code
- Your script must contain the following statements
random.seed(83)
FILENAME = "numbers.txt"
file = open(FILENAME, "w")
random_file_create(file, 50, 100, 20)
file = open(FILENAME, "r")
minimum = minimum_number(file)
print(minimum)
file = open(FILENAME, "r")
average = average_numbers(file)
print(average)
- They should apprear at the bottom of your script
Output
- The output should look something like this
52
66
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_file_create.
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 in
minimum_number.
Set min to 100.
Write a for
loop over file.
Inside the for
loop print each line.
If you don't do this, a blank line after each number.
Uncomment the next two lines in the test code.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Turn each line into an integer and assign it to
num.
Print num.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an if
statement that will run if
num is less than
min.
The body of the if statement should set min
to num and then
print min.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Outside the loop return min.
Uncomment the next line in the test code.
Run the script.
Fix any errors you find.
-
Remove the
pass
statement from
average_numbers.
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 1329.
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 20.
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 66.45.
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 last line in the test code.
Run the script.
The average should be 66.
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
52
66
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
52
66
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.