Counting vowels and consonants in a file (Python)

29,304
  1. set("A E I O U a e i o u") will result in {' ', 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'}. If you'll notice, the space is also considered. You'll need to remove the spaces between the letters.

  2. infile.read().split() will split based on whitespace so you get a list of words. You then proceed to iterate over the words, and try a membership comparison between the words and the letters. This will not work out for you.

  3. You don't need to iterate twice. Once is enough.


Here's a cleaned up version of your code.

vowels = set("AEIOUaeiou")
cons = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")

countV = 0
countC = 0
for c in infile.read():
    if c in vowels:
        countV += 1
    elif c in cons:
        countC += 1

As an improvement, consider the use of collections.Counter. It does the counting for you, and you just sum up the counts.

import collections
c = collections.Counter(infile.read())

countV = sum(c[k] for k in c if k in vowels)
countC = sum(c[k] for k in c if k in cons)
Share:
29,304
PyPunk
Author by

PyPunk

By day: Working at a local government help desk By night: Studying for my bachelors in Cyber Security with a dream of being a Pen Tester.

Updated on February 24, 2022

Comments

  • PyPunk
    PyPunk about 2 years

    I need to make a program that will read a text file and print how many vowels and consonants there are. I make a text file to test and the only thing in it is "This is a test". However the output it always:

    Enter the file to check: test.txt

    The number of Vowels is: 1

    The number of consonants is: 0

    fileName = input("Enter the file to check: ").strip()
    
    infile = open(fileName, "r")
    
    
    vowels = set("A E I O U a e i o u")
    cons = set("b c d f g h j k l m n p q r s t v w x y z B C D F G H J K L M N P Q R S T V W X Y Z")
    
    text = infile.read().split()
    
    
    countV = 0
    for V in text:
        if V in vowels:
            countV += 1
    
    countC = 0
    for C in text:
        if C in cons:
            countC += 1
    
    print("The number of Vowels is: ",countV,"\nThe number of consonants is: ",countC)
    

    If there is a better way to enter the values for vowels and cons I would also like to know, as I get an error when I try to user .lower() to convert everything in the file to lower case.....

    • Willem Van Onsem
      Willem Van Onsem almost 7 years
      Because you count spaces as well??
  • PyPunk
    PyPunk almost 7 years
    Thank you @COLDSPEED this helped. I did it slightly different but getting rid of the .split() and removing the spaces was a big help. I thought that sets don't repeat themselves though? So doesn't that mean that it would count the spaces too rather than showing 0 as a result?
  • cs95
    cs95 almost 7 years
    @EvanH It counts the spaces once, but they are still counted. You will have spaces erroneously counted as both vowels and characters unless you removed them.
  • PyPunk
    PyPunk almost 7 years
    @COLDSPEED Got it. Thank you very much for the help and quick response!