So I've written a code to get an acronym of the phrase inputed by the user. The only problem is that it insert an extra line in the result which I don't need. I'm not sure what is causing it. Here is the code and the results:

import string

def acronym(phrase):
x = ""
phrase = string.capwords(phrase)
for phrase in phrase.split():
x = x + phrase[0]
print "The acronym is", x
return acronym

def main():
phrase = raw_input("Enter a phrase: ")
print acronym(phrase)

main()


>>>
Enter a phrase: Dani Web
The acronym is DW
<function acronym at 0x00C4D1B0>
>>>

Recommended Answers

All 2 Replies

(1) use [code="Python"] [/code] tags to show indentation correctly.

(2) Your return value for the function acronym is the function acronym. Thus, the line

"print acronym(phrase)"

is printing the object acronym, which is a function. You want something like

import string

def acronym(phrase):
    x = ""
    phrase = string.capwords(phrase)
    for phrase in phrase.split():
        x = x + phrase[0]
    # No print statement here...that's bad form in (most) functions.
    return x  # << Note return value!!

def main():
    phrase = raw_input("Enter a phrase: ")
    print "The acronym is", acronym(phrase)

main()

Nevermind, the code worked just fine. Thanks so much!

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.