简体   繁体   中英

How to extract the first letter of certain words

I have an assignment that wants me to 'nickname' an input string. So far I was able to extract the first letter of every word in the string, but I need to exempt pronouns and words smaller than three characters.

This is what I have so far:

def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?: ')
    if name == "quit":
        print("bye")
    words = name.split()
    letters = [word[0] for word in words]
    return(" ".join(letters).upper())

try

def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?: ')
    if name == "quit":
        print("bye")
    words = name.split()
    letters = [word for word in words if len(word)>3]
    return(" ".join(letters).upper())
def nickname():
    name = input('Would you like to nickname a sentence or would you like to quit?:   ') 
    if name == "quit":
        print("bye")
    else:
      words = name.split()
      letters = [word[0] for word in words if len(word)>3]
      return("".join(letters).upper())

print(nickname())

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