我正在尝试实现一个程序,该程序计算从文件中读取的数字的平方和。
.txt 文件中的数字是:
37
42
59
14
25
95
6
到目前为止,我有:
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
当我运行程序时,我得到:
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