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:
Error:
I am stuck here, please help! Thank you.
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