Python Forum

Full Version: Counting only letters in a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm reading from a file with the following text:

Do you like green eggs and ham?
I like them, Sam I am!
Do you like green eggs and ham, Sam?
Do you like them, Sam I am?

Objective: Create an output file that contains every third character from the input file only. In addition, the character should be uppercase, if it is a consonant, and lowercase, if it is a vowel. Finally, the program should output statistics for the altered text, including: The number of characters, letters, consonants, and vowels

So the output file should look like this:

ui e Ga Mii e M !ooLeRNG Da MDY KTMS a
The number of characters: 39
The number of letters: 27
The number of consonants: 15
The number of vowels: 12

I got everything correct in my output file except the count for letters (28) and consonants (16), which is one more than what it's supposed to be. I believe it's counting the exclamation point, how do I exclude it from the count?
This is my code so far:
inputFile = input("Enter input file name ")
inputFile = open(inputFile, 'r')

output = input("Enter output file name ")
output = open(output, 'w')

chCount = 0
vowCount = 0
spaceCount = 0
chCountTwo = 0

for line in inputFile:
    for ch in line:
        chCount = chCount + 1
        ch = ch.upper()
        if chCount%3 == 0:
            if ch == ' ':
                spaceCount = spaceCount + 1
            elif ch == 'A' or ch == 'E' or ch == 'I' or ch == 'U' or ch == 'O':
                ch = ch.lower()
                vowCount = vowCount + 1  
            output.write(ch)
            chCountTwo = chCountTwo + 1
            letterCount = chCountTwo - spaceCount
            consoCount = letterCount - vowCount

str = 'The number of characters: '+str(chCountTwo)+"\n"+'The numer of letters: '+str(letterCount)+"\n"+'The number of consonants: '+str(consoCount)+"\n"+"The number of vowels: "+str(vowCount)
output.write("\n"+str)

inputFile.close()
output.close()
Some nitpicking:

I would be nice to pepify your code (make it compatible with PEP 8 Function and Variable names). Doing so will avoid lot of pitfalls in the future.

It is strongly encouraged to work with files using 'with' statement:

with input_file as f:
Instead of:

elif ch == 'A' or ch == 'E' or ch == 'I' or ch == 'U' or ch == 'O':
one can use:

elif ch in ['A', 'E', 'I', 'U', 'O']
Some ideas:

- string module has ascii_lowercase, ascii_uppercase, punctuation constants. You can take advantage of that
- there is chr() built-in function and ASCII codes for uppercase letters are in range(65, 91), lowercase letters are in range(97, 123). You take advantage of that.
Currently you are counting spaces, vowels, and everything else, and assuming everything else in consonants. This is where your problem is, since the exclamation mark is in everything else. Since you need counts of vowels and consonants, that's what you should count directly.