vertigo wrote: > Hello > > I receive such error: > File "p4.py", line 24, in PrintWordCountFloat > print "%s %f" % (word,words[word]) > TypeError: list indices must be integers > > i call PrintWordCountFloat with hash table, keys are words(string) and > values float. > This part of the code: > > def PrintWordCountFloat(words): > number = 0 > for word in words: > print "%s %f" % (word,words[word]) #line 24 > number = number + 1 > print "Total words: %d" %(number) > > My function displays whole table correctly, and after that i receive > mentioned error. > Why ? Where is the problem ?
Perhaps you meant something more along the lines of this: >>> def PrintWordCountFloat(words): number = 0 for index, word in enumerate(words): print "%s %f" % (index, word) number = number + 1 print "Total words: %d" %(number) >>> PrintWordCountFloat(range(10)) 0 0.000000 1 1.000000 2 2.000000 3 3.000000 4 4.000000 5 5.000000 6 6.000000 7 7.000000 8 8.000000 9 9.000000 Total words: 10 Or similar; I can't read your mind. Just know that enumerate(iterable) yields (index, value) for each item in iterable. > Thanx -- http://mail.python.org/mailman/listinfo/python-list