IT 116: Introduction to Scripting
Homework 8
Due
Sunday, April 6th 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
- Make sure the script has a hashbang line and is executable
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 file of pseudo_random numbers
- And count the odd numbers in a text file
- The script must contain 4 functions
- random_numbers_write
- count_odds
- largest_number
Functions
random_numbers_write
looping 10 times:
create a random number between 1 and 1000
convert this number to a string
write this number to the file adding a newline
count_odds
- Header
def count_odds(file):
- This function has one parameter,
file, a file object for reading
- It should loop through the file and count the number of
odd numbers it finds
- And return this number
- The function should use the following algorithm
set odd_count to 0
for each line in the file:
convert the line into an integer
if the number is odd:
add 1 to odd_count
return odd_count
largest_number
set largest to 0
for each line in the file:
convert the line into an integer
if this integer is greater than largest:
set largest to this number
return largest
Test Code
- Your script must contain the following statements
random.seed(67)
FILENAME = "numbers.txt"
file = open(FILENAME, "w")
random_numbers_write(file)
file = open(FILENAME, "r")
odds = count_odds(file)
print(odds)
file = open(FILENAME, "r")
max = largest_number(file)
print(max)
- They should appear at the bottom of your script
Output
- The output should look something like this
7
99
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
-
At the top of the script file 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.
-
In random_numbers_write replace the
pass
statement with a for
loop than runs
10 times.
Do this using a call to range
with argument 10.
Inside the for loop use an assignment
statement to create the variable number.
Give this variable a value by calling
random.randint with arguments of 1 and 100.
Print number.
Copy the test code to the bottom of your script.
Comment out all but the first 4 lines.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
In the step above, you created 10 random integers.
You need to write those numbers to a file.
But you need to turn those numbers into strings, because we are
creating text files.
Write an assignment statement that will turn
number into a string.
Print number.
Run the script.
Fix any errors you find.
-
Remove the
print
in the for
loop.
Replace it with a statement that will
write number followed by a linefeed
character, \n, to the file.
You need the linefeed character so each number is written to its own
line.
Run the script.
Look at the file numbers.txt .
It should contain the same numbers you saw in the previous step.
Fix any errors you find.
-
In count_odds remove the
pass
statement.
Set odd_count to 0.
Write a for
loop that prints every line in
file.
Uncomment the next two lines in the test code.
Run the script.
Fix any errors you find.
-
You need to convert each line in the file into a number.
Remove the print
statement.
Replace it with a 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 run if
number is odd.
If the remainder when you divide a number by 2 is 1, it is odd.
Inside the if
statement add 1 to
odd_count.
Print odd_count.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Outside the for
loop return
odd_count.
Uncomment the next line in the test code.
Run the script.
Fix any errors you find.
-
Remove the
pass
statement from
largest_number.
Replace it with a statement that sets
largest to 0.
Write a for
loop that prints every line in the file.
Uncomment the next two lines in the test code.
Run the script.
Fix any errors you find.
-
Remove the
print
statement inside the for
loop.
Replace it with a 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 runs if
number is greater than
largest.
Inside the if
statement, set
largest to number.
Print largest.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Outside the for
loop, return
largest.
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
7
99
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
7
99
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.