943,929 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1770
  • Python RSS
Apr 5th, 2009
0

Getting a function to accept String Input

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
murnshaw is offline Offline
3 posts
since Apr 2009
Apr 5th, 2009
0

Re: Getting a function to accept String Input

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).
Reputation Points: 11
Solved Threads: 4
Newbie Poster
Nine Tails is offline Offline
20 posts
since Mar 2009
Apr 5th, 2009
0

Re: Getting a function to accept String Input

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
murnshaw is offline Offline
3 posts
since Apr 2009
Apr 5th, 2009
0

Re: Getting a function to accept String Input

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')
Reputation Points: 11
Solved Threads: 4
Newbie Poster
Nine Tails is offline Offline
20 posts
since Mar 2009
Apr 6th, 2009
0

Re: Getting a function to accept String Input

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?
Python Syntax (Toggle Plain Text)
  1. >>> x = 'something'
  2. >>> x[::-1]
  3. 'gnihtemos'
  4. >>>
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Apr 6th, 2009
0

Re: Getting a function to accept String Input

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
murnshaw is offline Offline
3 posts
since Apr 2009
Apr 6th, 2009
0

Re: Getting a function to accept String Input

also for the last line of your code

python Syntax (Toggle Plain Text)
  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...
Reputation Points: 7
Solved Threads: 5
Light Poster
Arrorn is offline Offline
40 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Looking for quit command
Next Thread in Python Forum Timeline: Simple Maze Program, NEEDS HELP!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC