IT 116: Introduction to Scripting
Homework 9
Due
Sunday, November 9th 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
temps.txt from
/home/ghoffman/course_files/it116_files
to the directory on your machine that holds
hw9.py
Specification
Functions
mean_temp
- Header
def mean_temp(file):
- The function has one parameter, file,
a file object
- The function must read in the file with a
for
loop to get the temperature for each date
- It must turn the temperature value into an integer and use it
to calculate the mean, or average temperature
- It must return the average temperature, 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:
increment count
get the date and temp
turn temp an integer
add temp to total
find the mean by dividing total by count
return the mean using round to turn it into an integer
max_min_temp
- Header
def max_min_temp(file):
- The function must read in the file with a
for
loop to get the temperature for each date
- It must turn the temperature value into an integer and use it
to calculate the minimum and maximum temperatures
- The function should use the following algorithm
set max to 0
set min to 500
for each line in the file:
get the date and temp
turn temp an integer
if temp is greater than max:
set max to temp
if temp is less than min:
set min to temp
return max and min
Test Code
- Your script must contain the following statements
FILENAME = "temps.txt"
file = open(FILENAME, "r")
mean = mean_temp(file)
print(mean)
file.close()
file = open(FILENAME, "r")
max_min_temp(file)
file = open(FILENAME, "r")
max_temp, min_temp = max_min_temp(file)
print(max_temp, min_temp)
- They should appear at the bottom of your script
Output
- The output should look something like this
77
89 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 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 mean_temp.
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 mean_temp.
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
date and temp.
Print date and
temp.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an assignment statement that turns
temp into an integer using
int()
.
Next increment count.
Write a statement that adds temp 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
statement from
max_min_temp.
Define the variable max and set it to 0.
Define the variable min and set it to 500.
Print max and min.
Uncomment the next two lines in the test code.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that uses split
on
line to assign values to the variables
date and temp.
Print date and
temp.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an assignment statement that turns
temp into an integer using
int()
.
Write an if
statement that will run if
temp is greater than
max.
Inside the if
statement write an assignment statement
that sets max to the current value of
temp.
Print max.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an if
statement that will run if
tempis less than
min.
Inside the if
statement write an assignment statement
that sets min to the current value of
temp.
Print min.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Outside the for
loop return
max and min.
Uncomment the last lines 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
77
89 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 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 temps.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
77
89 66
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.