Python Forum
Counting only letters in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting only letters in a string
#1
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()
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,915 Jun-16-2020, 07:22 PM
Last Post: bowlofred
  Trying to extract Only the capitol letters from a string of text Jaethan 2 2,168 Feb-27-2020, 11:19 PM
Last Post: Marbelous
Exclamation A function that takes a word and a string of forbidden letters pooyan89 5 4,644 Jun-12-2019, 09:44 PM
Last Post: nilamo
  counting chars in string w/o len johneven 2 4,090 Mar-20-2019, 07:11 PM
Last Post: ichabod801
  slicing and counting words in a string Rudy 11 6,391 Mar-17-2019, 10:46 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020