IT 116: Introduction to Scripting
Homework 10
Due
Sunday, April 20th 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
rainfall.txt from
/home/ghoffman/course_files/it116_files.
Specification
- This script will read in a text file and create
a list of rainfall on different days
- It will use this list to compute the average
rainfall and the maximum rainfall
- The script must contain 3 functions
- rain_list_from_file
- average_rain
- max_rain
Functions
rain_list_from_file
average_rain
- Header
def average_rain(list):
- The parameter list is a list
of the rain that fell on each day
- The function must calculate the average to 2 decimal
places and return that value
max_rain
- Header
def max_rain(list):
- The parameter list is a list
of the rain that fell on each day
- The function must find the maximum rainfall in the list
and return that value
Test Code
Output
- The output should look something like this
1.23
6
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
-
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.
-
In rain_list_from_file remove the
pass
statement.
Write a statement that creates an empty list and assign it to the
variable list.
Now write a for
loop that loops through the file.
Inside the loop print each line.
Copy the test code to the bottom of the file.
Comment out the last 4 lines of the test code.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that assign values to the variables
date
and rain by running
split on line.
Now turn rain into an integer using
the int
conversion function.
Print these variables.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a line that appends rain
to list.
Outside the for
print list.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that returns list.
In average_rain remove the pass
statement.
Replace it with a statement that prints list.
Remove the # from the line in the test code
that calls average_rain.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that sets the value of the variable
total to 0.
Write a for
loop that loops through the list.
Inside the loop print each value in the list.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that adds the loop variable to
total.
Outside the loop, print 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 value average.
You should use the length of the list in calculating the average.
Print average.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that returns
average rounded to 2 decimal places.
Remove the # from the line in the test code
that prints average.
Run the script.
Fix any errors you find.
-
In max_rain remove the
pass
statement.
Replace with a statement that prints list.
Remove the # from the line in the test code
that calls max_rain.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a line that sets the value of
max to 0.
Write a for
loop that loops over the values in the list.
Inside the loop print each value.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with an if
statement that prints the loop
variable if it is larger than max.
Run the script.
Fix any errors you find.
-
Remove the
print
statement in the if
statement.
Replace it with a statement that sets max
to the loop variable.
Outside the for
loop, print max.
Run the script.
Fix any errors you find.
-
Remove the
print
statement.
Replace it with a statement that returns max.
Remove the # from 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 hw10.py
- Run your script inside IDLE
- Your output should look something like this
1.23
6
- 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 rainfall.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
python3 hw10.py
- You should see something like this
1.23
6
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.