200k views
1 vote
Write a program that lets the user enter in a file name (numbers.txt) to read, keeps a running total of how many numbers are in the file, calculates and prints the average of all the numbers in the file. This must use a while loop that ends when end of file is reached. This program should include FileNotFoundError and ValueError exception handling. Sample output: Enter file name: numbers.txt There were 20 numbers in the file. The average is 4.35

User Naeem
by
4.3k points

1 Answer

6 votes

Answer:

Check the explanation

Step-by-step explanation:

The code will be

name=input('Enter file name:')

ls=open(name,'r').read().split('\\')

count=0;sums=0

for i in range(ls):

sums+=int(i)

count+=1

print('There were {} numbers in the file'.format(count))

print('The average is {}'.format(sums/count))

The output is

Write a program that lets the user enter in a file name (numbers.txt) to read, keeps-example-1
User Lea Hayes
by
5.6k points