0
\$\begingroup\$

I was reading Introduction to Computation and Programming using Python and there in an exercise, we had to write a function which would tell us if a given word is a palindrome or not, recursively.

This is the code which I wrote:

word = input()

def isPalindrome(x):
    if len(x) > 1:
        if x[0] == x[-1] and isPalindrome(x[1:-1]):
            return('The word is Plaindrome.')
        else:
            return('It is not.')
    else:
        return(True) #As this is a base case.


print(isPalindrome(word))

This seems to work for all the words I pass into it except uiouioiu. isPalindrome(uiouioiu) returns The word is Palindrome.

Could someone explain what is happening?

EDIT 1: If I modify the code to:

word = input()

def isPalindrome(x):
    if len(x) > 1:
        if x[0] == x[-1] and isPalindrome(x[1:-1]):
            return(True)
        else:
            return(False)
    else:
        return(True) #As this is a base case.


print(isPalindrome(word))

isPalindrome(uiouioiu) returns False as expected. Why is this happening?

\$\endgroup\$

closed as off-topic by 200_success, Austin Hastings, vnp, AJNeufeld, esote Apr 25 at 4:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Austin Hastings, vnp, AJNeufeld, esote
If this question can be reworded to fit the rules in the help center, please edit the question.

1
\$\begingroup\$

The reason it doesn't work is that "It is not" is converted into True, so when you analyze "uiouioiu" the process eventually has to check the middle part "ui", the problem is that the answer "It is not" is then interpreted as True.

The same behaviour should also be obtained when testing the word "abca"

\$\endgroup\$
  • \$\begingroup\$ Hi. Thanks for the answer. Could you please explain what do you mean by " It is notis converted into True" ? It is not == True evaluates to False! \$\endgroup\$ – user199357 Apr 25 at 1:41
  • \$\begingroup\$ Check instead something like if('It is not'): do something \$\endgroup\$ – Jorge Fernández Apr 25 at 1:42
  • \$\begingroup\$ Put the argument 'It is not' inside an if statement and check if it does what it is supposed to do. \$\endgroup\$ – Jorge Fernández Apr 25 at 1:42
  • 2
    \$\begingroup\$ @RahulJ The concept you are looking for is called "truthiness" or "truthyness" and it is not limited to Python. See this blog post for an explanation. Be aware that similar (but different) rules exist in other languages, and it's the job of the programmer to know these rules. \$\endgroup\$ – Austin Hastings Apr 25 at 2:16
  • 5
    \$\begingroup\$ Please do not answer off-topic questions. \$\endgroup\$ – greybeard Apr 25 at 3:26