I've written a simple program in python which checks if the sentence is palindrome. But I can't figure out why isn't it working. The results is always False. Does anyone knows what's wrong?
def isPalindrome(word):
# Removes all spaces, and lowercase the word.
word = word.strip().lower()
word = word.replace(" ", "")
# If the length of the word is less than 1, means its a palindrome
if (len(word) <= 1):
return True
# Compares the first and the last character of the word.
# If it is the same, calls the function again with the same word,
# without its first and last characters. If its not the same, its
# not palindrome
else:
if word[0] == word[-1]:
isPalindrome(word[1:-1])
else:
return False
sentence = input("Enter a sentence: \n")
if (isPalindrome(sentence)):
print("The sentence %s is palindrome." % sentence)
else:
print("The sentence %s is NOT palindrome" % sentence)
You aren't returning the result of the function.
replace :
if word[0] == word[-1]:
isPalindrome(word[1:-1])
with
if word[0] == word[-1]:
return isPalindrome(word[1:-1])