(60 Points)
Speed Reading: Average all of the numbers from an input file
(perhaps with multiple numbers on some input lines).
This problem is a bit longer, requiring you to combine the ideas of:
-
defining functions,
-
reading from files,
-
while loops, and
-
using a graphics window to get user input.
Your task is to read numbers from a file and calculate the mean of the numbers. Unfortunately, the file is a bit malformed. The numbers are generated by a radar gun that measures the speed of each vehicle on the road coming up the hill from the SRC. Due to an intermittent open circuit occasionally multiple numbers are written on a single line, like this:
23.2 15.6 19.8
A sample input file, speeds.txt, is in your project.
Open the speedReading.py file in the
Session10 project that you checked out from SVN in class.
Modify it to calculate the arithmetic mean (average) of the numbers in a file. Your
program must conform to the following specification:
- Your program should use the four functions as given in
speedReading.py:
- lineSumAndCount()
- fileMean()
- getInputFile()
- main()
- When you write more complicated programs, you should be able to
test them as you go.
Typically, software developers write a set of tests for each small part of the program (thus they are called "unit tests").
We've given you unit tests for the first 2 functions in the file
unitTestsSpeedReading.py to help you. So,
- Write lineSumAndCount first
- Then run unitTestsSpeedReading.py to make sure it works.
- Then write and test fileMean in the same way.
- Finish up by creating the GUI and testing it on the file speeds.txt in your project
by running speedReading.py. The
average of the speeds in that file should be around 20.16.
-
Your program should ask the user for the name of the input file to process. Rather than using “console” input, your program should use a GUI similar to the one in Figures 5.9 and 5.10 on pages 150 and 151 of Zelle’s text book. Use an Entry object (Zelle, page 148) to accept the input file name from the user.
-
Your program should display the final average in the GUI window.
Hint: You can use the
setText()
method to change the displayed value of a string that has already been drawn.
- Yes, there are many other ways
that you could write this. We're asking you to follow our specification so you can get practice working with the sort of situations that come up in real world, group projects, where you
often must implement someone else's design design.
We also think that doing it this way will get you to
confront some issues and clear up some misunderstandings
that you may have about functions.
- Commit your file to your SVN repository.