Search code examples
pythonstringpalindromesplice

Testing for a Palindrome in Python


I now know there are better solutions for this, but I'm confused as to why I'm getting the result I am.

import sys

def isPalindrome(test):
    if len(test) == 1:
        return("Is a palindrome")
    else:
        if test[0] == test[-1]:
            isPalindrome(test[1:-1])
        else:
            return("Not a palindrome")        

print(isPalindrome(sys.argv[1]))

On a true palindrome, I get 'None'. When the result is not a palindrome, I get the expected value of 'Not a palindrome'.


Solution

  • Change to the following line:

    return isPalindrome(test[1:-1])
    

    You have to return a value or the value returned is None.