In: Computer Science
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1.
I need to define the average function
Define the main function which will include the following things
1. prompt user for input of text file name
2. open and read input file, can be done before or inside high order functions
3. use two high order functions
4.calculate and display averages and original ist
5. call the main
6. exit the program
Here is what i have so far:
#Write a program that computes and prints the average of numbers in
a text file."
import functools
#define average functions
def average(numbers):
"""returns the average of the numbers in list"""
if len(numbers) == 0:
return 0
#Find the total sum and return the average
theSum = 0
for number in numbers:
theSum += number
return theSum/ len(numbers)
#define main
def main():
#prompt user input of text file name
filename = input("Enter a file name: ")
#open and read input file
file = open(filename, "r")
filename = file.read().split()
#convert list into integers
file = list(map(int,file))
#calculate and display average(s) and display original list
print(average(numbers))
#call main
if __name__ == "__main__" :
main()
Am i on the right path? I always get hung up on calling the function in the main. Please help me understand this process better. Thanks
from functools import reduce
def getNumberList(filename):
f = open(filename,'r')
line = f.readline()
numbers = line.split(',')#split the numbers separated by comma
#higher order function to convers string data into integer
numberList = list(map(lambda x : int(x),numbers))
return numberList
def getAverage(numbers):
#higher order function reduce to find the sum of elements
sum = reduce(lambda x, y: x + y, numbers)
average = sum/len(numbers)
return average
def main():
#take input from the user
filename = input("Enter filename : ")
#get the numbers from the file
numbers = getNumberList(filename)
#get the average from the numbers list
average = getAverage(numbers)
#display the average
print(average)
if __name__ == "__main__":
main()
(Pratyushs-MacBook-Pro:python pthapli$ python3 test.py Enter filename : integers.txt 3.0 Pratyushs-MacBook-Pro:python pthaplis | integers.txt 1,2,3,4,5
1 2 3 4 5 6 def getNumberList(filename): f = open(filename, 'r') line = f.readline() numbers line.split(',')#split the numbers separated by comma numberlist []#holds the integer value for i in numbers: numberList.append(int(i)) 7 8 9 return numberlist 10 11 12 13 14 15 16 17 def getAverage (numbers): sum = 0#stores the sum of the numbers in the list count = O#keeps the count of numbers in the list for i in numbers: sum = sum + i count = count + 1 18 average = sum/count#calculate the average return average 19 20 21 22 23 24 25 26 27 28 29 30 def main(): #take input from the user filename input("Enter filename: ") #get the numbers from the file numbers = getNumberList(filename) #get the average from the numbers list average = getAverage(numbers) #display the average print(average) 31 IL if _main_": 32 33 34 _name main()