In: Computer Science
I am trying to make a program in Python that opens a file and tells you how many lines, vowels, consonants, and digits there are in the file.
I got the print out of lines, digits, and consonants, but the if statement I made with the vowels list isn't recognizing them. How can I make the vowels go through the if statement with the list? Am I using the wrong operator?
Here is my program
#Assignment Ch7-1
#This program takes a user input file name and returns how many
lines, vowels,
#consonants and digits are in the
while True:
vowel = 0
cons = 0
digit = 0
count = 0
vlist = ('aeiou')
try:
fname = input('Please
enter the file name to open:')
fhand =
open(fname)
for line in fhand:
count += 1
for ch in line:
if ch.isdigit():
digit +=1
elif ch.isalpha():
if ch is vlist:
vowel += 1
else:
cons += 1
print('This is the
vowels: ',vowel)
print('These are the
digits: ',digit)
print('These are the
consanants: ',cons)
print('These are the
lines: ',count)
ans = input("Do you want
try again y/n: ") #asking user to contiune
if ans == 'y' or ans ==
'Y':
continue
else:
if ans == 'n' or ans == 'N':
print('Goodbye')
break
except:
print('File cannot be
opened:', fname)
continue
Please find the fixed code below :
while True:
vowel = 0
cons = 0
digit = 0
count = 0
vlist = ('aeiou')
try:
fname = input('Please enter the
file name to open:')
fhand = open(fname)
for line in fhand:
count += 1
for ch in
line:
if ch.isdigit():
digit +=1
elif ch.isalpha():
if ch in vlist:
vowel +=
1
else:
cons +=
1
print('This is the vowels:
',vowel)
print('These are the digits:
',digit)
print('These are the consanants:
',cons)
print('These are the lines:
',count)
ans = input("Do you want try again
y/n: ") #asking user to contiune
if ans == 'y' or ans == 'Y':
continue
else:
if ans == 'n' or
ans == 'N':
print('Goodbye')
break
except:
print('File cannot be opened:',
fname)
continue
Sample Output :
Sample File:
Sample Output:
Please
comment below if you have any queries.
Please do give a thumbs up if you liked the answer thanks
:)