Search code examples
pythoncounterfrequencyletter

Count the number of times a letter is used


With this I not only need to count the frequency of the letters in a word, but I also need to spit out the unused letters.

alphabetList = ["a","b","c","d","e","f","g","h","i","j","k",\
    "l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]


def Checksums():
    for i in alphabetList:

        character = i
        myString = Phrase
        myList = []

        for i in myString:
            myList.append(i)

        myList = myList.sort()
        print myList

        count = 0
        for i in myList:
            if i == character:
                count = count +1
            else:
                continue
        print("There are ", count, " occurrences of ", character )


#input
Phrase = str(raw_input("Enter a Phrase: "))

Phrase = Phrase.lower()
# print (Phrase)
Phrase = Phrase.replace(" ", "")
# print Phrase

Checksums()

An example of input could be:

aaA cC D <br>

and the returning would be

"There were '3' occurrences of the letter 'a'"<br>
"There were '2' occurrences of the letter 'c'"<br>
"Only 1 'd'"<br>
"The remaining letters are unused": b, e, etc...

I tried to use the alphabet list and just cycle through that but I'm getting the following error:

TypeError: 'NoneType' object is not iterable


Solution

  • replace myList = myList.sort() by myList.sort() in your function.