HI, I'm having some trouble with this program - its supposed to return an acronym based on the input of the user: It prints three of the same letter instead of the acronym. Any help would be much appreciated.

def acronym(phrase):
    a = ""
    for words in phrase.split():
        a = a + phrase[0]
    return a

def main():
    print("This program creates an acronym for a phrase")
    print("entered by the user.")

    text = input("Please enter your phrase: ")
    text = text.upper()

    answer = acronym(text)

    print(str(answer))


main()

Recommended Answers

All 4 Replies

Replace phrase[0] with words[0].

commented: Thanks! I can't believe I was making such a dumb mistake. +0

Gribouillis is spot on but I would also move text = text.upper() into your acronym method too - its cleaner that way and more reusable.

Here is bit more advanced version when you start to internalize the Python philosophy:

# Lets make it run with Python2
from __future__ import print_function

try:
    input = raw_input
except:
    pass

def acronym(phrase):
    return ''.join(word[0] for word in phrase.upper().split())

def main():
    print("This program creates an acronym for a phrase")
    print("entered by the user.")
    print(acronym(input("Please enter your phrase: ")))


main()

Did this solve your problem? If so, please mark it as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.