| | |
Getting a function to accept String Input
![]() |
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Mar 2009
Posts: 20
Reputation:
Solved Threads: 4
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).
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
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?
Are you trying to print a string backwards?
Python Syntax (Toggle Plain Text)
>>> x = 'something' >>> x[::-1] 'gnihtemos' >>>
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Mar 2009
Posts: 23
Reputation:
Solved Threads: 4
also for the last line of your code
this does the exact same thing and is simpler to write...
python Syntax (Toggle Plain Text)
index = index + 1 #you could simplify this to index += 1
this does the exact same thing and is simpler to write...
![]() |
Similar Threads
- Linked List: Mp3 Menu (String and Delete problem) (C++)
- Anagram Tester Program That I Just Can't Figure Out (Computer Science)
- input file (C++)
- User Input without GUI... Need Guidance (Java)
- Terminating a String (C)
- Reverse string arrays (C)
- Green underlined words (DaniWeb Community Feedback)
- New Problem with input output in Tk (Python)
- Pascal starter. (Pascal and Delphi)
- Convert first character from lowercase to uppercase? (C++)
Other Threads in the Python Forum
- Previous Thread: launching a wxpython gui from matlab
- Next Thread: Simple Maze Program, NEEDS HELP!
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib





