1

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!')

moosegumps
  • 43
  • 4
  • In your own words, what do you expect `if vwl is 'a' 'A' 'e' 'E' 'i' 'I' 'o' 'O' 'u' 'U' 'y' 'Y' :` to mean? How and why? – Karl Knechtel Nov 08 '20 at 05:25
  • There are multiple issues I see with the code, but the most important ones both happen to be common issues with established reference/duplicate answers. The problem occurs because `char` is empty (since you have already read the file with the `for line` loop), 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 with `int()` makes any sense. – Karl Knechtel Nov 08 '20 at 05:31
  • "I believe that Python counts the strings line by line using the open function but requires the .read() function to iterate through individual characters." This description, however, is a mess. Getting code right requires clear thinking, and it helps to start with accurate use of terminology. For example, Python doesn't "count" anything; the program logic counts the lines, by doing `count += 1`, `for` each of the `line`s. – Karl Knechtel Nov 08 '20 at 05:32

1 Answers1

1

Here's a solution that uses python's built in re package for handling regular expressions.

First, I'll define functions to count the number of vowels and numbers in a given string:

import re

def find_vowels(text):
    pattern = re.compile("[aeiouAEIOU]")
    results = re.findall(pattern, text)
    return len(results)

def find_numeric(text):
    pattern = re.compile("[0-9]")
    results = re.findall(pattern, text)
    return len(results)

This code defines regular expression patterns that are then matched to the text. Each match is returned as part of a list, and I then return the length of that list as the value that you're looking for.

Now I can define another function that uses user input to open a text file and go through it line by line, counting lines, vowels, and numbers as it goes with the help of our previously defined functions:

def txtcount():
    vowel_count = 0
    numeric_count = 0
    fname = input('Please enter file name: ')
    with open(fname) as file:
        for i, l in enumerate(file):
            vowel_count += find_vowels(l)
            numeric_count += find_numeric(l)
    print("Lines: {}\nVowels: {}\nNumbers: {}".format(i+1, vowel_count, numeric_count))

I wrote this scintillating piece of literature to test my code:

I am a text file
I have a whole bunch of lines
1, 2, 3, count how many!

When I put txtcount() at the end of my code and run it in the command line I get the following output:

Lines: 3
Vowels: 20
Numbers: 3

I am indebted to this excellent question for my approach to getting the line count in an efficient manner.

Also, it's well worthwhile to read the python docs on re here.

James Tollefson
  • 883
  • 3
  • 14