A202 / I211 Assignment 7
Iterative applications
Due Thursday, October 21st, 3:00 PM
Pair programming in this assignment if you like. You may
choose any partner in your lab, or work on your own if you prefer.
In lab
- Take your statistics.py program, or the posted solution to
assignment 2, and save it as lab7.py.
- Modify this program so that it prompts for input and terminates
when an empty string is input (by just pressing the Enter key in
response to the input prompt).
- Next modify the program so it is an application that takes a file
name argument and reads the data from the file, with one number per
line in the file. Comment-out any lines from part 2 that need to be
modified or are not appropriate for this part. To test your program, create a file data.txt
with one number per line. You may use the IDLE text editor, or any
other plain-text editor to create this file. (Note: editors such as
Word that support text formatting normally produce files in formats
that are not plain text.)
C:\home\202\a\7\type data.txt
81
58
73.5
C:\home\202\a\7>python lab7.py data.txt
Minimum = 58.0
Maximum = 81.0
Range = 23.0
Mean = 70.8333333333
Standard deviation = 11.7295922066
When you have completed the last exercise above, or 15 minutes before the
lab ends, whichever comes first, submit your lab7.py file as lab 7
in Vincent. (The file you submit will reflect your effort on part 3 above, not
part 2.) If you have time, start the assignment below.
Assignment
Write an application named tempDays (in a file named
tempDays.py, of course) that solves programming problems 11 and 12
of chapter 8, with a few additions, as follows:
- First implement problem 11, prompting for input and using an empty input
string as the end-of-input sentinel. This can be run from the IDLE using F5,
or as an application that takes no arguments. Accumulate the heating and
cooling days as floating-point values, but print them at the end as integers.
Also at the end print the average temperature with one digit after the decimal
point.
- Next convert it into an application that optionally takes one argument,
which is the input file name. Read the file one line at a time (not all at
once using readlines), assuming there is one number per line. If there are no arguments, the application should prompt for input as in
part 1. Avoid duplicating code when possible.
- Finally, modify your application so it takes any number of arguments. If
there is zero or one argument, proceed as above. If there are two more more
arguments, assume they are all numbers and use them as the input. Try to do
all this with just one loop that contains conditional tests to determine where
the input is coming from.
- Now make the application robust. If the file name does not name a
readable file, give an appropriate error message and quit. If any strings,
s, that is to be converted to a temperature numbers is not numbers, print
the message Bad number: s and get the next number. For this
purpose, consider a number to be any sequence of digits with zero or one
decimal point, possibly with whitespace at the beginning and/or end.
Define a predicate (function that returns a boolean value) isNumber
that takes a string and indicates if it is a number as defined here. Do not
use a try statement. Check out the string module documentation
for a function that will help, and also be prepared to iterate over the
elements of the string.
C:\home\202\a\7>python tempDays.py
Enter temperature (<Enter> to quit): 58
Enter temperature (<Enter> to quit): 81
Enter temperature (<Enter> to quit): 73.t
Bad number: 73.t
Enter temperature (<Enter> to quit): 73.5
Enter temperature (<Enter> to quit):
Cooling days: 1
Heating days: 2
Average temperature: 70.8
C:\home\202\a\7>python tempDays.py data.txt
Cooling days: 1
Heating days: 2
Average temperature: 70.8
C:\home\202\a\7>python tempDays.py 58 81 73.t 83.5
Bad number: 73.t
Cooling days: 5
Heating days: 2
Average temperature: 74.2
When you are done, submit your final tempDays.py file as
a7 using Vincent.