0

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>
>>>

2
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by MakingMoney
0

(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()
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.