0

could someone please indicate to me what's wrong with the logic of the simple recursive function, below:

def palindrome (string):

if len(string)<=1:
    return True
elif string[0]== string[-1]:
    palindrome(string[1:-1])
else:
    return False

the False part, works ok, for example : palindrome('good') the function returns correctly False. but if it's fed a palindrome like : palindrome('level') the function returns nothing.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Yassine
  • 1
  • 2

1 Answers1

0

As mentioned in the comments, you don't return the function when calling it recursively, but you should, or the original function call will return None.

Solution:

def palindrome (string):
    if len(string)<=1:
        return True
    elif string[0].lower() == string[-1].lower():
        return palindrome(string[1:-1])
    else:
        return False

I would also add "lower" as capitalization isn't relevant

ᴓᴓᴓ
  • 1,178
  • 1
  • 7
  • 18