- #1
- 7,376
- 11,342
- TL;DR Summary
- Algorithm correctly reverses numerical strings but does not recognize letter strings
Hi,
I have a simple Python algorithm in my Jupyter notebook , to reverse a string.
It works well on numerical strings, but when I try to reverse a letter string, I get an error message,
e.g., the string abc, I get the error message:
name 'abc' is not defined
This is my Python code
It works no problem with numerical input, e.g., if I enter:
reverse(-235), it returns -532
But if I try reverse(abc), I get the error message above:
"Name 'abc' not defined"
I have no idea what the issue could be. Any ideas?
I have a simple Python algorithm in my Jupyter notebook , to reverse a string.
It works well on numerical strings, but when I try to reverse a letter string, I get an error message,
e.g., the string abc, I get the error message:
name 'abc' is not defined
This is my Python code
Python:
def reverse(x):
String=str(x)
if String[0]=='-':
return( '-' + str[:0:-1])
else:
return( str[::-1])
reverse(-235), it returns -532
But if I try reverse(abc), I get the error message above:
"Name 'abc' not defined"
I have no idea what the issue could be. Any ideas?