7— File Data Statistics
In this project, you will write a program to read a series of values from a file and calculate the following statistics:
• A count of the number of values read from the file
• The sum (total) of the values read from the file
• The average (total / count) of the values read from the file
• The largest number read from the file
• The smallest number read from the file
Example of program in operation:
Reading data from file...
Read complete!
Count: 10
Total: 786
Average: 78.6
Highest: 100
Lowest: 48
Development Process
A template for this project is provided with comments to help guide you as you write the program to accomplish the following steps:
1. Download both the template and data file for Program 7 and save them to the same folder on your computer/flash drive.
2. View/print the following programs from the Chapter 8 and Chapter 10 course modules in Blackboard as examples to follow as you complete this program:
a. Program 10-6 – open file in read mode; read data; add to total
b. Program 8-11 – finding the highest (max) value
c. Program 8-12 – finding the lowest (min) value
3. Open the template. Below the Step 1 comment, write a line of code to open the “Prog7Data.txt” file in read mode. The variable name on the left side of the equal sign is the file’s internal name and will be used in the rest of your code when referencing the file. For example, in Program 10-6 the file is opened and given the internal name videoFile.
4. Below the Step 2 comment, write a line of code to read the first line from the file and to store the string into a variable.
5. Below the Step 3 comment, write the first line of a while loop to test if the end of the file has been reached. Remember in Python that a null string (“”) indicates the end of the file. The code for Steps 4-9 will be indented under the while loop.
6. Below the Step 4 comment (note the indentation), write a line of code to convert the string value stored in the variable to a float value, then store the result back into the same variable.
7. Below the Step 5 comment (note the indentation), write a line of code to add the float value from the variable in Step 4 to the variable named total and store the result back into total (i.e. this is a running total/accumulator).
8. Below the Step 6 comment (note the indentation), write a line of code to add 1 to the count accumulator variable. This variable simply adds 1 to count for each line read from the file.
9. Below the Step 7 comment (note the indentation), write an if statement to test if the float value in the variable from Step 4 is the new “low” value. See Program 8-12 for an example of this logic.
10. Below the Step 8 comment (note the indentation), write an if statement to test if the float value I the variable from Step 4 is the new “high” value. See Program 8-11 for an example of this logic.
11. Below the Step 9 comment (note the indentation), write a line of code to read the next line from the file and to store the string into a variable. NOTE: This should look the same as the code from Step 2, but will be indented under the loop.
12. Below the Step 10 comment (not indented), write a line of code to close the file.
13. Below the Step 11 comment, write a series of print statements to display the count, total, average, highest and lowest.

Template for Program 7
Complete the code as discussed in the Program 7 Instructions
Accumulators to hold count and running total

count = 0
total = 0

Variables to hold the current highest and lowest values

highest = -1000
lowest = 1000

STEP 1 - Open the data file named prog7Data.txt in read mode
Print a message to let the user know what is happening

print("Reading data from file...")

STEP 2 - Read the first line from the file to get things started
STEP 3 - Use a while loop to test if the end of file has been reached

while

#STEP #4 - Convert the value read from the file to a float


#STEP #5 - Add the value to the running total


#STEP #6 - Add 1 to the counter


#STEP #7 - Check for new low value


#STEP #8 - Check for new high value


#STEP #9 - Read the next line from the input file
STEP #10 - Close the file
STEP #11 - Print the summary statistics (count, total, average, etc.)

and this is the data
76
82
29
29
33
57
91
55
36
35
13
32
40
80
75
94
11
57
75
19

I HAVE NO IDEA WHERE TO EVEN START!!! Someone please help me :(

Here is a hint ...

data_str_raw = '''\
76
82
29
29
33
57
91
55
36
35
13
32
40
80
75
94
11
57
75
19'''

# write the data to a file
fname = "aa_numbers.txt"
with open(fname, "w") as fout:
    fout.write(data_str_raw) 

# read the data back in
with open(fname, "r") as fin:
    data_str = fin.read()

# check the data and type
for item in data_str.split('\n'):
    print(type(item), item)

# do some processing
number_list = []
number_total = 0
for item in data_str.split('\n'):
    # convert from string to integer
    number = int(item)
    # add up the numbers
    number_total += number
    # build the list
    number_list.append(number)

print("total = {}".format(number_total))
# or
print("total = {}".format(sum(number_list)))
print("items in list = {}".format(len(number_list)))

# if you use Python2 / is a integer division, so force a float with
#average = 1.0*sum(number_list)/len(number_list)
average = sum(number_list)/len(number_list)
print("average = {}".format(average))

# show the list
print(number_list)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.