•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,543 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,319 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 832 | Replies: 2
![]() |
•
•
Join Date: Sep 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 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>
>>>
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>
>>>
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
(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
(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
Python Syntax (Toggle Plain Text)
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()
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Code 19 Registry Error (Windows NT / 2000 / XP / 2003)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the Python Forum
- Previous Thread: Inheritance from other objects
- Next Thread: newbie text game


Linear Mode