Python palindrome program not working

后端 未结 7 814
后悔当初
后悔当初 2021-01-26 12:49

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 know

7条回答
  •  臣服心动
    2021-01-26 13:43

    You algorithm is fine, the only problem is that you don't return the true result through the recursion, you have to return the isPalindrome result when calling it recursively:

    else:
        if word[0] == word[-1]:
            return isPalindrome(word[1:-1]) #this changed
        else:
            return False
    

提交回复
热议问题