Question: Refactor the following code (rewrite it to work the same way but be better) to improve it based on the best-practices you have been taught,
Refactor the following code (rewrite it to work the same way but be better) to improve it based on the best-practices you have been taught, including the Single Responsibility Principle (SRP) for function design. Also, briefly explain how you have refactored it and why. How is the code better in your new version?
def main(): in_file = open("data.txt") data = get_data(in_file) in_file.close() average(data, len(data))
def get_data(in_file): values = [] for line in in_file: values.append(float(line)) return values
def average(data, length): average = sum(data) / length print("The average is", average) main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
