palindrome iteration

Terry Reedy tjreedy at
Fri Aug 27 19:39:08 CEST 2010

On 8/27/2010 4:53 AM, Baba wrote:
> level: beginner
> the following code looks ok to me but it doesn't work. I would like
> some hints as to where my reasoning / thought goes wrong
> def i_palindrome(pal):
>   while len(pal)>1:
>    if pal[0] == pal[-1]:
>     pal=pal[1:-1]
>   return True
> print i_palindrome('annab')

General practical debugging procedurewhen logic inspection fails: insert 
print statements at key points.

In the case above, put "print pal" before the if statement and you 
should see the problem. And/or "print 'equal'" after the if.

Terry Jan Reedy

More information about the Python-list mailing list