IT 116: Introduction to Scripting
Homework 9
Due
Sunday, April 7th at 11:59 PM
What You Need to Do
- Create the script hw9.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 hw9
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 hw9.py
- Using FileZilla, copy the file
id_scores.txt from
/home/ghoffman/course_files/it116_files
to the directory on your machine that holds
hw9.py
Specification
Functions
average_score
- Header
def average_score(file):
- The function has one parameter,
file, a file object
- The function must read in the file with a for loop to
get the score for each student
- It must turn the score into an integer and
use it to calculate the average score
- It must return the average score, rounded to an
integer
- The function should use the following algorithm
set count to 0
set total to 0
for each line in the file:
get the values for id and score
turn score into an integer
add score to total
increment count by 1
find the average by dividing total by count
return the average using round to turn it into an integer
highest_lowest_score
set max_score to 0
set min_score to 100
for each line in the file:
get the values for id and score
turn score into an integer
if score is greater than max_score:
set max_score to score
if score is less min_score:
set min_score to score
return max_score, min_score
Test Code
- Your script must contain the following statements
FILENAME = "id_scores.txt"
file = open(FILENAME, "r")
average = average_score(file)
print(average)
file = open(FILENAME, "r")
highest_lowest_score(file)
file = open(FILENAME, "r")
highest, lowest = highest_lowest_score(file)
print(highest, lowest)
Output
- The output should look something like this
73
99 40
- The text in blue is what the
user enters
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.
Copy only the function headers for the two 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 from average_score.
Create the variables count and
total giving them initial values of 0.
Print the variables total and
count.
Copy all of the test code to the bottom of your script.
Comment out all but the first three lines of the test code.
Do this by inserting # at the beginning of
the line.
Run the script.
Fix any errors you find.
-
Remove the print statement from average_score.
In it's place write a for
loop that prints every line in
the file.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that calls split
on
the line to assign values to the variables
id and score.
Print id and
score.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an assignment statement that turns
score into an integer using
int()
.
Next increment count.
Write a statement that adds score to
total.
Outside the for loop, print count and
total.
Run the script.
Fix any errors you find.
-
Remove the print statement.
Replace it with a statement that calculates the average and assigns
it to the variable average.
The value of average is a float.
Use an assignment statement to make average
an integer using round
.
Return average.
Uncomment the first commented line in the test code.
Run the script.
Fix any errors you find.
-
Remove the pass from highest_lowest_score.
Create the variable max_score and give it
the value of 0.
Create the variable min_score and give it
the value 100.
Print the variables max_score and
min_score.
Uncomment the next three lines in the test code.
Run the script.
Fix any errors you find.
-
Remove the print statement from
highest_lowest_score.
In it's place write a for
loop that prints every line in
the file.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that calls split
on
the line to assign values to the variables
id and score.
Print id and
score.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an assignment statement that turns
score into an integer using
int()
.
Write an if
statement that will run if
score is greater
than max_score.
Inside this if
statement set
max_score to score.
Also inside the if
statement print
max_score.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
After the 1st if
statement write another
if
statement that will run if
score is less
than min_score.
Inside this if
statement set
min_score to score.
Also inside the if
statement print
min_score.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Outside the for
loop return
max_score and
max_score.
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 hw9.py
- Run your script inside IDLE
- Your output should look something like this
- The text in blue is what you
enter at the command line
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 hw9 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
- Click and drag id_scores.txt 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/hw9
- Run this script
./hw9.py
- You should see something like this
73
99 40
Copyright © 2024 Glenn Hoffman. All rights reserved. May not be reproduced without permission.