-3

im trying to convert a program that takes the users string input and defines how many lowercase, uppercase, digits, and characters the string has. Into a program that reads a text file and outputs lower/uppercase/digits/and characters. I have most of the program done as fas as print outputs but I cant figure out why my .readlines() isnt reading every line in the file and analyzing them instead of only looking through the first line.

def countList( myList ):
    if len(myList) == 0:
        return 0

    #variables
    totalChars = 0
    digitCount = 0
    spaceCount = 0
    alphaCount = 0
    upperCase  = 0
    lowerCase  = 0
    notDigitOrAlphaOrSpace = 0

    
    for ch in myList:     
        totalChars+=1
        if ch.isdigit():
            digitCount+=1
        elif ch.isspace():
            spaceCount+=1
            if ch.isupper():
                upperCase+=1
            else:
                lowerCase+=1
        else:
            notDigitOrAlphaOrSpace+=1
    #output
    print("Total characters:\t\t",totalChars)
    print("Total digits:\t\t",digitCount)
    print("Total spaces:\t\t",spaceCount)
    print("\tAlpha upper:\t",upperCase)
    print("\tAlpha lower:\t", lowerCase)
    print("Not an alpha or digit or space:\t", notDigitOrAlphaOrSpace)
    return totalChars
def main():
   
    myFile = open('text.txt','r')
    myList = myFile.readline()
    # call our count the characters function
    numChars = countList(myList)

    
    if numChars > 0:
        print("Your file was",numChars,"characters long")
    else:
        print("Your file had no characters.")

main()
user3697625
  • 167
  • 4
  • 17
Adrian
  • 1
  • 5
    You are calling `readline` instead of `readlines`. – Sayandip Dutta Feb 19 '21 at 16:18
  • Please format the code - select it and type `ctrl-k`. .. [Formatting help](https://stackoverflow.com/editing-help)... [Formatting sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii Feb 19 '21 at 16:25

1 Answers1

3

Call file.readlines() instead of readline() to read all the lines

nimish
  • 323
  • 1
  • 5