[PyAR2] Mini Programming Challenge

W W srilyk at gmail.com
Thu Oct 16 19:14:20 CEST 2008


I've actually done this before, though I'm not sure where my exercise went,
but it was fairly simple so I'll see if I can write it in here :D

mystr = raw_input("Please enter a string: ")
mycount = {}

for letter in mystr:
    try:
        mycount[letter] += 1
    except KeyError:
        mycount[letter] = 1

print mycount

That should be about as simple as you can get (though it will count
whitespace and other characters).

this should work for only alpha characters. If you want to ignore case, just
add .lower() after .isalpha()

mystr = raw_input("Please enter a string: ")
mycount = {}

for letter in mystr:
    if letter.isalpha():
        try:
            mycount[letter] += 1
        except KeyError:
            mycount[letter] = 1

print mycount

-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/pyar2/attachments/20081016/77cc53fc/attachment.htm>


More information about the PyAR2 mailing list