I am creating a program that will take user input and read a txt file to print number of lines, vowels, and numeric characters within it, repeating until the user does not want to execute it again. I am able to get the correct print for lines, however I am getting 0 for vowels and numeric characters. I believe that Python counts the strings line by line using the open function but requires the .read() function to iterate through individual characters. As such, I have assigned char as the variable for that iteration.
def txtcount():
count = 0
vowels = 0
numerical = 0
fname = input('Please enter file name: ')
fopen = open(fname)
for line in fopen:
count +=1
char = fopen.read()
for number in char:
if number.isnumeric():
numerical = int(number) + 1
for vwl in char:
if vwl.isalpha():
if vwl is 'a' 'A' 'e' 'E' 'i' 'I' 'o' 'O' 'u' 'U' 'y' 'Y' :
vowels += int(vwl) + 1
else :
vowels = int(vwl) + 0
print('File',fname,'has', count, 'lines.')
print('File',fname,'has', vowels, 'vowels')
print('File',fname,'has', numerical, 'numerical characters\n')
while True:
txtcount()
tryagain = input('Do you want to try again? (y or n)').lower()
if tryagain == 'y' :
continue
if tryagain == 'n' :
break
print('Thanks for playing!')
if vwl is 'a' 'A' 'e' 'E' 'i' 'I' 'o' 'O' 'u' 'U' 'y' 'Y' :to mean? How and why?charis empty (since you have already read the file with thefor lineloop), and after that you will need proper logic for the vowel membership test. After that, the remaining problem is that your tallying logic for the vowels and digits is incorrect - you want to count them the same way you count the lines. None of that stuff withint()makes any sense.count += 1,foreach of thelines.