Getting a function to accept String Input

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 3
Reputation: murnshaw is an unknown quantity at this point 
Solved Threads: 0
murnshaw murnshaw is offline Offline
Newbie Poster

Getting a function to accept String Input

 
0
  #1
Apr 5th, 2009
Sorry for the noobish question, but I'm trying to write a function that accepts strings as inputs. Here's what I've got:

def backwards(x):
word = "x"
index = 1
while index < len(word)+1:
letter = word[-index]
print letter
index = index + 1

Whenever I call the function, such as backwards(word), it gives me a name undefined error. How do I get it to accept the input?

Thanks for the help!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 20
Reputation: Nine Tails is an unknown quantity at this point 
Solved Threads: 4
Nine Tails Nine Tails is offline Offline
Newbie Poster

Re: Getting a function to accept String Input

 
0
  #2
Apr 5th, 2009
In python variable types are not important. You should just assign x to your string and strike the line word = "x". This assigns word to the string x not to the string of the value stored in the variable x as you were probably intending. You also want to start with index zero since python starts iterable objects at position zero (this would also cause you to remove the +1 in your loop statement).
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: murnshaw is an unknown quantity at this point 
Solved Threads: 0
murnshaw murnshaw is offline Offline
Newbie Poster

Re: Getting a function to accept String Input

 
0
  #3
Apr 5th, 2009
Thank you for the quick response. I modified the function and removed word but I'm getting the same error. Now the function looks like this:

def backwards(x):
index = 0
while index < len(x):
letter = x[-index]
print letter
index = index + 1


I'm getting the same error:

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
backwards(something)
NameError: name 'something' is not defined
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 20
Reputation: Nine Tails is an unknown quantity at this point 
Solved Threads: 4
Nine Tails Nine Tails is offline Offline
Newbie Poster

Re: Getting a function to accept String Input

 
0
  #4
Apr 5th, 2009
When you enter the word something without quotation marks or apostrophes python thinks that you are referencing a variable. This is why you are getting the error. In order to pass the function the string something you need to call the function as follows:

backwards('something')
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Getting a function to accept String Input

 
0
  #5
Apr 6th, 2009
I think NineTrails solved the error. You should use code tags. Your code would be easier to read.

Are you trying to print a string backwards?
  1. >>> x = 'something'
  2. >>> x[::-1]
  3. 'gnihtemos'
  4. >>>
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: murnshaw is an unknown quantity at this point 
Solved Threads: 0
murnshaw murnshaw is offline Offline
Newbie Poster

Re: Getting a function to accept String Input

 
0
  #6
Apr 6th, 2009
Putting the input in quotes worked! Thank you guys so much! Yes, I'm trying to write a function that prints words backwards (Working through "How to Think Like a Computer Scientist" by myself). I knew the problem had to do with the input and now I know what it is! This is one lesson I won't soon forget; I've been trying to figure it out all week.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 23
Reputation: Arrorn is an unknown quantity at this point 
Solved Threads: 4
Arrorn Arrorn is offline Offline
Newbie Poster

Re: Getting a function to accept String Input

 
0
  #7
Apr 6th, 2009
also for the last line of your code

  1. index = index + 1
  2.  
  3. #you could simplify this to
  4.  
  5. index += 1

this does the exact same thing and is simpler to write...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC