New to python and programming in general, I'm struggling to get this program to run correctly. It has to:
- Read a text file, if the file doesn't exist it has to direct the person to re-input the text file. I have this part working.
- Count the lines, words in each line, and vowels in each line and output those amounts to a text file.
- Count the total number of words and vowels and output that at the bottom of the text file.
- I can't import anything other than OS for this. I don't know how to use the list function but I could probably use it in the code, essentially if I haven't used it and it's a better option I probably haven't learned it in class yet.
So the input test file says :
"This is a
test file
to see if
the program works."
The output text file should say:
Line Number: 1
Number of vowels in line 1: 3
Number of words in line 1: 3
Line Number: 2
Number of vowels in line 2: 3
Number of words in line 2: 2
// etc. Then at the bottom:
Total number of vowels in the file: 14
Total number of words in the file: 11
Instead, depending on the loop statement I try to use I either get infinite lines, two lines, or 8 lines. And the count for words and vowels is off.
Here's what I have so far based on what we've learned in class:
fileIn = open(input("File name: "), 'r')
fileOut = open("answer.txt", 'w')
numLines = 0
totVowels = 0
totWords = 0
    
for line in fileIn:
    line = fileIn.readline()
    if line != "":
        numLines += 1
        numWords = len(line.split(" "))
        fileOut.write("Line number: %0d" % numLines)
        fileOut.write("\n")
        numA = line.count('A')
        numE = line.count('E')
        numI = line.count('I')
        numO = line.count('O')
        numU = line.count('U')
        numLa = line.count('a')
        numLe = line.count('e')
        numLi = line.count('i')
        numLo = line.count('o')
        numLu = line.count('u')
        numVowels = numA + numE + numI + numO + numU + numLa + numLe + numLi + numLo + numLu
        fileOut.write("Number of vowels in line %0d: %0d" %(numLines, numVowels))
        fileOut.write("\n")
        fileOut.write("Number of words in line %0d: %0d" %(numLines, numWords))
        fileOut.write("\n")
        fileOut.write("\n")
    else:
        for lines in fileIn.readlines():
            words2 = lines.split()
            totWords += len(words2)
            if 'a' in words2:
                totVowels += 1
            elif 'e' in words2:
                totVowels += 1
            elif 'i' in words2:
                totVowels += 1
            elif 'o' in words2:
                totVowels += 1
            elif 'u' in words2:
                totVowels += 1
            elif 'A' in words2:
                totVowels += 1
            elif 'E' in words2:
                totVowels += 1
            elif 'I' in words2:
                totVowels += 1
            elif 'O' in words2:
                totVowels += 1
            elif 'U' in words2:
                totVowels += 1
            else:
                fileOut.write("\n")
                fileOut.write("+++++++++++++++++")
                fileOut.write("\n")
                fileOut.write("Total number of vowels in file: %0d" % totVowels)
                fileOut.write("\n")
                fileOut.write("Total number of words in file: %0d" % totWords)
                
                        
print("answer.txt file created.")
fileIn.close()
fileOut.close()
I honestly am really lost at this point, I've been going through my notes frantically trying to figure out what I'm missing but I'm hitting a wall, I'm tapped out mentally. I feel like I'm being thrown in the deep end but still haven't learned to swim yet. Any help or advise is appreciated.
 
     
    