User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2007
Posts: 6
Reputation: MakingMoney is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MakingMoney MakingMoney is offline Offline
Newbie Poster

Acronym code

  #1  
Oct 12th, 2007
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>
>>>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Acronym code

  #2  
Oct 12th, 2007
(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

  1.  
  2. import string
  3.  
  4. def acronym(phrase):
  5. x = ""
  6. phrase = string.capwords(phrase)
  7. for phrase in phrase.split():
  8. x = x + phrase[0]
  9. # No print statement here...that's bad form in (most) functions.
  10. return x # << Note return value!!
  11.  
  12. def main():
  13. phrase = raw_input("Enter a phrase: ")
  14. print "The acronym is", acronym(phrase)
  15.  
  16. main()
Reply With Quote  
Join Date: Sep 2007
Posts: 6
Reputation: MakingMoney is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MakingMoney MakingMoney is offline Offline
Newbie Poster

Re: Acronym code

  #3  
Oct 13th, 2007
Nevermind, the code worked just fine. Thanks so much!
Last edited by MakingMoney : Oct 13th, 2007 at 12:49 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 5:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC