On Mon, Aug 24, 2009 at 10:48 AM, kreglet <kreg...@gmail.com> wrote: > > Wayne, > > The reason I used print sorted is that using just print throws a syntax > error: > > print (lettercount.iteritems(), key=itemgetter(1)) ---> error > print lettercount.iteritems(), key=itemgetter(1) ---> error > print sorted(lettercount.iteritems(), key=itemgetter(1)) ---> works
Ah, excuse me - I just glanced at your function and didn't notice you weren't returning any value. You should be returning lettercount from your function. > > > I don't know why. Seems to me that any of the above should work. > > mainword = countletters('batty') > cmpword = countletters('bat') > > myfunc(cmpword, mainword) > > Generates error: > > Traceback (most recent call last): > File "/home/kreglet/bin/test.py", line 23, in <module> > myfunc(cmpword, mainword) > File "/home/kreglet/bin/test.py", line 13, in myfunc > for letter in cmpword: > TypeError: 'NoneType' object is not iterable > > mainword = countletters('batty') > print mainword returns None > > cmpword = countletters('bat') > print cmpword returns None > > Both mainword and cmpword are passed to the function but since the values > of > each are None > the function doesn't work. > > Is this correct? > That's correct - your function countletters returns None, so you're assigning cmpword to None. And since None is not an iterable type, it throws an error. Try returning a value like I mentioned above and see how that works. HTH, Wayne > > thanx, > kreglet > > > Wayne-68 wrote: > > > > On Sun, Aug 23, 2009 at 10:01 PM, kreglet <kreg...@gmail.com> wrote: > > > > I would actually not bother sorting your return from countletters - keep > > it > > a dictionary. > > > > Then you can compare like this: > > > > mainword = countletters('batty') > > cmpword = countletters('bat') > > > > def myfunc(cmpword, mainword): > > for letter in cmpword: > > if mainword.gets(letter): > > if cmpword[letter] >mainword[letter]: > > return False > > else: > > return False > > > > I think that should work. First you're looping over each letter in > > cmpword. > > Because mainword is also a dictionary the order isn't terribly important. > > Then you check if the letter is in mainword. If it's not, obviously > > cmpword > > isn't in mainword, so return False. If the letter is, compare the counts. > > If > > cmpword has more letters than mainword, it's not in the word so again > > return > > False. > > > > HTH, > > Wayne > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > -- > View this message in context: > http://www.nabble.com/Algorithm-tp25107922p25118434.html > Sent from the Python - tutor mailing list archive at Nabble.com. > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor