Jul-19-2018, 07:18 PM
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:
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()