Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help for B. Answer for A is below: def average_numbers_in_file(filename): # Open the file for reading with open(filename, 'r') as f: # Initialize a
Need help for B. Answer for A is below:
def average_numbers_in_file(filename): # Open the file for reading with open(filename, 'r') as f: # Initialize a variable to hold the total sum of the numbers total_sum = 0 # Initialize a variable to hold the number of numbers read num_numbers = 0 # Read each line in the file for line in f: # Split the line into a list of numbers numbers = line.split(',') # Convert the strings to integers and add them to the total sum total_sum += sum(int(x) for x in numbers) # Increment the number of numbers num_numbers += len(numbers) # Return the average of the numbers return total_sum / num_numbersA) Write a python function that will accept a name of a text file containing multiple rows with comma separated numbers (for example " 5,6,12,56,1 ") and return the average of these numbers. Your function should compute and average all of the numbers in a file. Note that your function should return the result of the computation (i.e., return res not print(res)). B) Write a python function that will accept a name of a text file containing multiple rows with comma separated numbers (same as in part-A) and will write these numbers into a new file so that there is only one number in each line
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started