Victoria Homsy wrote:

> Sorry to bother you with a beginner's problem again...

This is the place for beginners.
 
> I have tried to write a program that can check if a string is a
> palindrome. My code is as follows:
> 
> 
> def isPalindrome(s):
>     if len(s) <= 1: return True
>     else: return s(0) == s(-1) and isPalindrome (s[1:-1])
> isPalindrome('aba')
> 
> 
> However, when I try to run it in terminal I get the following error
> message:
> 
> Traceback (most recent call last):
> File "recursion.py", line 5, in <module>
> isPalindrome('aba')
> File "recursion.py", line 3, in isPalindrome
> else: return s(0) == s(-1) and isPalindrome (s[1:-1])
> TypeError: 'str' object is not callable
> 
> 
> I don't see why this wouldn't work...

If you want to get the nth charactor you have to put the index in brackets, 
not parens:

>>> s = "foo"
>>> s(0) # wrong, python tries to treat s as a function
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> s[0] # correct
'f'


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to