Question

In: Computer Science

Write a program that computes and prints the average of numbers in a text file. I...

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

Solutions

Expert Solution

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()


Related Solutions

Write a JavaScript program that computes the average of a collection of numbers and then outputs...
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
in c++ (Sum, average and product of numbers in a file) Suppose that a text file...
in c++ (Sum, average and product of numbers in a file) Suppose that a text file Exercise13_3.txt contains six integers. Write a program that reads integers from the file and displays their sum, average and product. Integers are separated by blanks. Instead of displaying the results on the screen, send the results to an output named using your last name. Example:       Contents of Exercise13_3.txt: 100 95 88 97 71 67 80 81 82             Contents of YourLastName.txt: Your...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
/ File: temperature.cxx // This program prints a table to convert numbers from one unit to...
/ File: temperature.cxx // This program prints a table to convert numbers from one unit to another. // The program illustrases some implementation techniques. #include // Provides cout #include // Provides setw function for setting output width #include // Provides EXIT_SUCCESS #include // Provides assert function using namespace std; // Allows all standard library items to be used double celsius_to_fahrenheit(double c) // Precondition: c is a Celsius temperature no less than absolute // zero (-273.16). // Postcondition: The return value...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
The following code searches a text file for definitions and prints them out. I want to...
The following code searches a text file for definitions and prints them out. I want to create a function that can be called for the given vector and map. The function will either print the results in reverse order, print results without duplicate definitions, or do both. What is the best approach to make the code less repetitive. if (find(keyWords.begin(), keyWords.end(), v[0]) != keyWords.end()) {                        for (auto map_iter = mymap.cbegin(); map_iter != mymap.cend(); ++map_iter)...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT