IT 116: Introduction to Scripting
Homework 10
Due
Thursday, October 23rd at 11:59 PM
What You Need to Do
- Create the script hw10.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 hw10
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 hw10.py
- Using FileZilla, copy the file
temps.txt from
/home/ghoffman/course_files/it116_files
to the directory on your machine that holds
hw10.py
Specification
Functions
temp_list_from_file
mean_temp
- Header
def mean_temp(list):
- The function has one parameter, list,
a list of temperatures
- The function must read in the list 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 temp in the list:
increment count
turn temp into 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(list):
- The function must read in the list 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 temp in the list:
turn temp into 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")
temp_list = temp_list_from_file(file)
mean = mean_temp(temp_list)
print(mean)
max_min_temp(temp_list)
max_temp, min_temp = max_min_temp(temp_list)
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 three 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
statement from
temp_list_from_file.
Replace it with a statement that creates the empty list
list.
Write a for
loop that prints every line in
the file.
Add the text code to the bottom of the script.
Comment out all but the first three lines of the test code.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that uses split
on
the line to assign values to the variables
date and temp.
Print temp.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that appends temp
to list.
Outside the for
loop print
list.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that returns list.
Remove the pass
statement from
mean_temp.
Replace it with a statement that sets total
to 0.
Print the parameter list.
Uncomment the next line in the test code.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a for
loop over
list using temp
as the loop variable.
Inside the loop print 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()
.
Add temp to total.
Outside the for
loop print
total.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that sets average
to total divided by the length of
list.
Print average.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a return statement that returns
average turned into an integer using
round
.
Uncomment the next line in the test code.
Run the script.
Fix any errors you find.
-
Remove the
pass
statement from
max_min_temp.
Set max to 0.
Set min to 500.
Print max and max.
Uncomment the next line in the test code.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a for
loop that runs over
list using the loop variable
temp.
Inside the loop print temp.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that converts
temp to an integer using int()
.
Write an if
statement that will run if
temp is greater than
max.
Inside the if
statement set max
to temp.
Print max.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Write an if
statement that will run if
temp is less than
min.
This statement must be outside the first if
statement
but inside the for
loop.
Inside this newest if
statement print set
min to 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 hw10.py
- Run your script inside IDLE
- Your output should look something like this
77
89 66
- 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 hw10 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/hw10
- Run this script
./hw10.py
- You should see something like this
77
89 66
Copyright © 2022 Glenn Hoffman. All rights reserved. May not be reproduced without permission.