Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribedError:

image text in transcribed

I am stuck here, please help! Thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions

Question

=+Trainers from headquarters? Local trainers? Independent trainers?

Answered: 1 week ago