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.....