I’m trying to implement a program that computes the sum of the squares of numbers read from a file.
The numbers in the .txt file are:
37
42
59
14
25
95
6
So far I have:
def squareEach(nums):
nums = nums * nums
def sumList(nums):
nums = nums + nums
def toNumbers(strList):
while line != "":
line = int(line)
line = infile.readline()
def main():
print "** Program to find sum of squares from numbers in a file **"
fileName = raw_input("What file are the numbers in?")
infile = open(fileName, 'r')
line = infile.readline()
toNumbers(line)
squareEach(line)
sumList(line)
print sum
When I run the program through I get:
main()
** Program to find sum of squares from numbers in a file **
What file are the numbers in?C:\Users\lablogin\Desktop\mytextfile.txt
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
main()
File "<pyshell#16>", line 6, in main
toNumbers(strList)
NameError: global name 'strList' is not defined
Normally, I would include a much more detailed explanation, but since I have to run, I’ll leave you with an amended version of your code that will work
Of course, you could do all of it in one line:
Hope this helps