-1

I was writing a code to find the average household income, and how many families are below poverty line.

this is my code so far

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

Call the main function.

povertyLevel()

I am trying to find the average of annualIncome and what i tried to do was

avgIncome = (sum(annualIncome)/len(annualIncome)) outFile.write(avgIncome)

i did this inside the while lineRead. however it gave me an error saying

avgIncome = (sum(annualIncome)/len(annualIncome)) TypeError: 'float' object is not iterable

currently i am trying to find which household that exceeds the average income.

2 Answers2

4

avgIncome expects a sequence (such as a list) (Thanks for the correction, Magenta Nova.), but its argument annualIncome is a float:

annualIncome = float(words[1])

It seems to me you want to build up a list:

allIncomes = []
while lineRead != '':
    ...
    allIncomes.append(annualIncome)

averageInc = avgIncome(allIncomes)

(Note that I have one less indentation level for the avgIncome call.)

Also, once you get this working, I highly recommend a trip over to https://codereview.stackexchange.com/. You could get a lot of feedback on ways to improve this.

Edit:

In light of your edits, my advice still stands. You need to first compute the average before you can do comparisons. Once you have the average, you will need to loop over the data again to compare each income. Note: I advise saving the data somehow for the second loop, instead of reparsing the file. (You may even wish to separate reading the data from computing the average entirely.) That might best be accomplished with a new object or a namedtuple or a dict.

Community
  • 1
  • 1
jpmc26
  • 23,237
  • 9
  • 76
  • 129
  • @spuriousarbiter I've added some notes to my answer in response to your edits. – jpmc26 Dec 04 '14 at 00:19
  • Two minor nits: First, it's `iterable`, not `iteratable`. More importantly, this _won't_ work on an arbitrary iterable; what you want here is a _sequence_. (While `sum` will work on any arbitrary iterable, `sum(x)/len(x)` will not, for two reasons: `len` only works on _sized_ collections, which not all iterables are, and the whole thing only makes sense for non-iterator iterables, because otherwise `x` will be empty after the `sum` call. Which "sequence" isn't quite the right thing either, it's usually close enough for practical purposes.) – abarnert Dec 05 '14 at 19:14
  • Anyway, I've edited the answer to say `sequence`; feel free to revert and fix it better if you want to, of course. – abarnert Dec 05 '14 at 19:14
  • @abarnert "Iteratable" was a typo; thanks for the corrections. – jpmc26 Dec 05 '14 at 20:01
2

sum() and len() both take as their arguments an iterable. read the python documentation for more on iterables. you are passing a float into them as an argument. what would it mean to get the sum, or the length, of a floating point number? even thinking outside the world of coding, it's hard to make sense of that.

it seems like you need to review the basics of python types.

Magenta Nova
  • 662
  • 8
  • 14
  • Well, if you knew that Python considers tuples to be sequences, and knew that mathematically a single number is the same thing as a 1-tuple of that number (but didn't know that wasn't true in Python), you might expect `sum(2.3)` to be `2.3`… But that probably applies to 2 people in the universe, and probably not the asker. :) – abarnert Dec 03 '14 at 02:56
  • @abarnert I don't think a 1-tuple is the same as a number. A tuple is defined as an ordered list of elements. A 1-tuple is a list of length 1, whereas a number isn't a list at all. The difference is as big as a set with 1 element or a vector with 1 dimension from a standalone number. – jpmc26 Dec 03 '14 at 03:02
  • @jpmc26: A Python 1-tuple of a single number obviously isn't the same as a number, but the way tuples are usually defined in set theory—or, for that matter, in programming langauges like Haskell—they are the same. The type of a tuple of 3 integers is `int * int * int`; the type of a tuple of an integer and a float is `int * float`; the type of a tuple of just an integer is `int`. Of course in such languages, there is not function to get the length or sum of a tuple, because such tuples aren't sequences (in the sense that lists are). – abarnert Dec 05 '14 at 19:09
  • @MagentaNova: As I mentioned to jpmb26 in his answer, [`len`](https://docs.python.org/3/library/functions.html#len) does _not_ take any iterable, it takes a sequence or other sized collection. For example, if you feed in a generator or other iterator, or a third-party single-linked-list type that's iterable but not sized, this isn't going to work. – abarnert Dec 05 '14 at 19:17
  • @abarnet: thanks for refining the point, but did that really merit a downvote? – Magenta Nova Dec 05 '14 at 20:32