title: python: recursive check to determine whether string is a palindrome
body: <p>My task is to define a procedure is_palindrome, that takes as input a string, and returns a boolean indicating if the input string is a palindrome. In this case a single letter should return True, as should an empty string <code>''</code>. </p>
  
  <p>Unfortunately, I'm not getting the expected results. I appreciate the help. </p>
  
  <p>My code version 1:</p>
  
  <pre><code>def is_palindrome(s):
      if s == '':
          return True
      else:
          if (ord(s[0]) - ord(s[len(s)-1])) == 0:
              is_palindrome(s[1:len(s)-1])
          else:
              return False
  
  print is_palindrome('')
  #&gt;&gt;&gt; True    (expected = True)
  
  print is_palindrome('abab')
  #&gt;&gt;&gt; False    (expected = False)
  
  print is_palindrome('abba')
  #&gt;&gt;&gt; None    (expected = True)
  
  print is_palindrome('andrea')
  #&gt;&gt;&gt; None    (expected = False)
  
  print is_palindrome('abaaba')
  #&gt;&gt;&gt; None    (expected = True)
  </code></pre>
  
  <p>I followed my code through the debugger and it seems the logic is correct as the code takes the appropriate path. However, the end result seems to switch to 'None' for some of the cases as highlighted above.</p>
  
  <p>If I change my code to the following:</p>
  
  <p>My code version 2:</p>
  
  <pre><code>def is_palindrome(s):
          if s == '':
              result = True
          else:
              if (ord(s[0]) - ord(s[len(s)-1])) == 0:
                  is_palindrome(s[1:len(s)-1])
              else:
                  result = False
          return result
  
  print is_palindrome('')
  #&gt;&gt;&gt; True    (expected = True)
  
  print is_palindrome('abab')
  #&gt;&gt;&gt; False    (expected = False)
  
  print is_palindrome('abba')
  #&gt;&gt;&gt; Error    (expected = True)
  UnboundLocalError: local variable 'result' referenced before assignment 
  
  print is_palindrome('andrea')
  #&gt;&gt;&gt; Error   (expected = False)         
  UnboundLocalError: local variable 'result' referenced before assignment
  
  print is_palindrome('abaaba')
  #&gt;&gt;&gt; Error    (expected = True)
  UnboundLocalError: local variable 'result' referenced before assignment
  </code></pre>