Generate acronym from phrase I've posted the code for the following question. What could be improved?


  Q: Write a program that allows the user to type in a phrase and then
  outputs the acronym for that phrase.
  
  Note: The acronym should be all uppercase, even if the words in the
  phrase are not capitalized.


def main():

    ## input the phrase
    phrase = input("Enter the phrase: ")

    ## split the phrase into substrings
    phrase_split = phrase.split()


    acronym = ""

    ## iterate through every substring
    for i in phrase_split:
        acronym = acronym + i[0].upper()

    print("The acronym for your phrase is ",acronym + ".")

main()
You should name your function more descriptively, maybe acronym.
You should wrap your code under a if __name__ == "__main__": guard to allow importing parts of your script from other scripts.
You should separate the user input from the logic.
Use docstrings to describe what functions do.
Use str.join together with a generator expression to join multiple strings.
Use the new f-strings introduced in Python 3.6.


Taking this gives you:



def user_input():
    """Get the user input for a phrase"""
    return input("Enter the phrase: ")

def acronym(words):
    """Turn a list of words into an acronym"""
    return "".join(word[0] for word in words).upper()

def main():
    words = user_input().split()
    print(f"The acronym for your phrase is {acronym(words)}.")

if __name__ == "__main__":
    main()