| | |
Getting a function to accept String Input
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog decimals dictionaries dictionary directory drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib





