-
Notifications
You must be signed in to change notification settings - Fork 23
/
file_numbers_average.py
57 lines (47 loc) · 1.89 KB
/
file_numbers_average.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
################################################################################
#
# Program: Find The Average Of Numbers In Each Line Of A File
#
# Description: Finds the average of the numbers contained on each line of a file
# using Python. Expected file format is a single number on each line of the
# file such as:
#
# 20
# 42
# 51
# 12
#
# YouTube Lesson: https://www.youtube.com/watch?v=FdySDb1XboI
#
# Author: Kevin Browne @ https://portfoliocourses.com
#
################################################################################
# returns the average of the numbers stored one per line in the file with the
# filename provided as an argument
def file_average(filename):
# opens the file for reading, file will be a reference to the file object
file = open(filename)
# readlines() will read each line from the file and store it as a string in
# a list
number_list = file.readlines()
# We'll need to total the numbers in the file, so create a variable total and
# initialize it to 0
total = 0
# Loop through each number in number_list and add it to the total, note that
# because each line of the file (i.e. each number) is stored as a string we
# conver it to a floating-point number with float() before adding it to the
# total.
for number in number_list:
total = total + float(number)
# Close our access to the file since we are done working with
file.close()
# len() will return the number of elements in the list (i.e. the total amount
# of numbers), we divide the total of the numbers by the total amount of
# numbers to compute and return the average.
return total / len(number_list)
# Prompt the user to enter the filename, store it into input_filename
input_filename = input("File: ")
# Use the function with the supplied filename to compute the average
average = file_average(input_filename)
# Output the average
print("Average:", average)