pythoncharactercount

Python 3.0 - How do I output which character is counted the most?


So I was able to create a program that counts the amount of vowels (specifially e i o) in a text file I have on my computer. However, I can't figure out for the life of me how to show which one occurs the most. I assumed I would say something like

for ch in 'i':
    return numvowel?

I'm just not too sure what the step is. I basically want it to output in the end saying "The letter, i, occurred the most in the text file"

def vowelCounter():
    inFile = open('file.txt', 'r')
    contents = inFile.read()

    # variable to store total number of vowels
    numVowel = 0

    # This counts the total number of occurrences of vowels o e i.
    for ch in contents:
        if ch in 'i':
            numVowel = numVowel + 1
        if ch in 'e':
            numVowel = numVowel + 1    
        if ch in 'o':
            numVowel = numVowel + 1

    print('file.txt has', numVowel, 'vowel occurences total')
    inFile.close()

vowelCounter()

Solution

  • If you want to show which one occurs the most, you have to keep counts of each individual vowel instead of just 1 total count like what you have done.

    Keep 3 separate counters (one for each of the 3 vowels you care about) and then you can get the total by summing them up OR if you want to find out which vowel occurs the most you can simply compare the 3 counters to find out.