This program is suppose to take 3 functions and implement it into one program th
ID: 3629804 • Letter: T
Question
This program is suppose to take 3 functions and implement it into one program that computes the sum of the squares of numbers read from a file. The 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 professor said it should look something like this:
main():
ask for file name
read the file and produce a list of strings
toNumbers()-should be the first function, converts the list of strings into number(probably by the float command)
squareEach()-this function squares each of the numbers
sumList()-this function sums of the squares
here is what i have so far if it helps:(i think part of my problem is naming things)
Explanation / Answer
Sorry I had a typo: s/b: s=sumList(a) not a=sumList(a)
def toNumber(line):
return float(line)
def squareEach(x):
y=[]
for i in x:
y.append(i**2)
return y
def sumList(x):
sum=0.0
for i in range(len(x)):
sum=sum+x[i]
return sum
def main():
fileName=raw_input("Enter filename:")
infile=open(fileName,'r')
a=[]
for line in infile.readlines():
a.append(toNumber(line))
a=squareEach(a)
s=sumList(a)
print "The sum of the squared values are: ",s
infile.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.