Question: Chapter 6, exercise 14: Use the function from the previous three problems to implement a program that computes the sum of the squares of numbers

Chapter 6, exercise 14:

Use the function from the previous three problems to implement a program that computes the sum of the squares of numbers read from a file. Your program should prompt for a file name and print out the sum of the squares of the values in the file. Hint: use readlines()

My code:

def main():

print("If you are not giving the full path, then that file must be present in the same directory.")

filename = input("Please input the file name:")

sum_of_squares(filename)

def sum_of_squares(filename):

fopen = open(filename, 'r')

sum = 0

for items in fopen.readlines():

items = int(items)

sum += items**2

print("The final sum is:", sum)

main()

Code Screenshot:

Chapter 6, exercise 14: Use the function from the previous three problemsError:

to implement a program that computes the sum of the squares of

I am stuck here, please help! Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!