我正在编写一个代码来查找平均家庭收入,以及有多少家庭处于贫困线以下。
这是我到目前为止的代码
def povertyLevel():
inFile = open('program10.txt', 'r')
outFile = open('program10-out.txt', 'w')
outFile.write(str("%12s %12s %15s\n" % ("Account #", "Income", "Members")))
lineRead = inFile.readline() # Read first record
while lineRead != '': # While there are more records
words = lineRead.split() # Split the records into substrings
acctNum = int(words[0]) # Convert first substring to integer
annualIncome = float(words[1]) # Convert second substring to float
members = int(words[2]) # Convert third substring to integer
outFile.write(str("%10d %15.2f %10d\n" % (acctNum, annualIncome, members)))
lineRead = inFile.readline() # Read next record
# Close the file.
inFile.close() # Close file
调用主函数。
贫困水平()
我试图找到年收入的平均值,而我试图做的是
avgIncome = (sum(annualIncome)/len(annualIncome)) outFile.write(avgIncome)
我在 while lineRead 中做到了这一点。但是它给了我一个错误说
avgIncome = (sum(annualIncome)/len(annualIncome)) TypeError: 'float' object is not iterable
目前我正在寻找超过平均收入的家庭。