Question: give a program that matches this example. Calculating the Average of Numbers in a File # Step 1 : Prompt the user for the file
give a program that matches this example. Calculating the Average of Numbers in a File
# Step : Prompt the user for the file name
filename inputEnter the name of the file containing numbers:
try:
# Step : Open the file in read mode
with openfilename, r as file:
# Step : Read the numbers from the file
numbers floatlinestrip for line in file
# Step : Calculate the average
if numbers:
average sumnumbers lennumbers
# Step : Print the average
printfThe average of the numbers in the file is: average:f
else:
printThe file is empty. No numbers to calculate."
except FileNotFoundError:
printThe specified file does not exist. Please try again."
except ValueError:
printThe file contains invalid data. Please ensure the file only has numbers."
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
