简体   繁体   中英

Reading subject in Python and counting upper and lower case letters

Subject: SAVE YOUR MUSIC AND REDEEM A SPOTIFY GIFT CARD

My assignment question asks to show the subject line from the above email and then to count the number of upper and lower case letters. I wrote the below code but nothing comes up. How do I fix the code?

efile = open('email.txt', 'r')
eMail = efile.readlines()
eSubject = 'Subject :'
for i in eMail:
    if eSubject in i:
        i = i[len(eSubject):]
            
        upperCase = 0
        lowerCase = 0
           
        for word in i:
            if word.isupper():
                upperCase += 1
            elif word.islower():
                lowerCase += 1
            else:
                pass 
               
        print('Subject is:', i)
        print('Upper case letters in subject:', upperCase)
        print('Lower case letters in subject:', lowerCase)

Subject: SAVE YOUR MUSIC AND REDEEM A SPOTIFY GIFT CARD is the line you are checking, but in the code the line is Subject : . Notice the space after Subject and before : . Try changing eSubject='Subject :' to eSubject='Subject:'

Also, the str class has a function called startswith that checks only the beginning of the string. So you can also do if i.startswith(eSubject): instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM