1
def palindrome(w):
    if len(w)==0 or len(w)==1:
        p = 'yes'
        return p
    else:
        if w[0]==w[-1]:
            del w[0]
            del w[-1]
            palindrome(w)
        else:
            p = 'no'
            return p           
s=input()
w=list(s.replace(" ",""))
print(palindrome(w))

when input is not a palindrome, the code works fine. but when the input is a palindrome. it is returning the value none instead of yes. why is that?

ps:- I'm a beginner-level programmer. I'm still learning. so please be kind enough to explain me more straightforwardly.

  • Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – Mechanic Pig Jul 14 '23 at 06:29
  • line no 9, you need to return what `palindrome` function returns. Replace line 9 with `return palindrome(w)`. For example, for input [1, 1] (which is a palindrome), function control will come to line 9, call the function with empty list(because you deleted first and the last element), this call will return "yes", because list size is 0. But this "yes" is not returned back to it's caller. – satinder singh Jul 14 '23 at 06:32
  • I assume this is merely an exercise in recursion because this approach for determining a palindrome is woefully inefficient – DarkKnight Jul 14 '23 at 07:27

2 Answers2

1

You will have to propagate the result of your palindrome(w) call:

return palindrome(w)

If a function ends without a return being executed, the function will automatically return the value None. Thus, e.g. if you call palindrome(["a", "b", "a"]), you will evaluate the subcall palindrome(["b"]), then discard its result, and return None for the value of palindrome(["a", "b", "a"]). If you use return palindrome(w), then the value of palindrome(["a", "b", "a"]) will become the value of palindrome(["b"]), which is the desired logic.


The following are not errors, just good practices.

It is probably better to make the function more general. Instead of making a palindrome function that can only handle lists, why not make it so it can handle any sequence, including strings directly? However:

del w[0]
del w[-1]

This does not work on strings, and will give you a TypeError: 'str' object doesn't support item deletion. You cannot use del to remove characters in a string: Python strings are immutable. You have to create a new string without the first and last character instead:

w = w[1:-1]

Now the function will work both for palindrome([1, 2, 3]) and palindrome("abc").

Finally, it would be better to use the standard Boolean type for "yes" and "no". If you need to display yes or no to the user, implement this logic outside the function that performs the test, preferably at the very last moment before you need to display it.


The final form:

def palindrome(w):
    if len(w)==0 or len(w)==1:
        return True
    else:
        if w[0]==w[-1]:
            w = w[1:-1]
            return palindrome(w)
        else:
            return False

s = input()
w = s.replace(" ","")
if palindrome(w):
    print("yes")
else:
    print("no")
Amadan
  • 191,408
  • 23
  • 240
  • 301
0

You should return a boolean value, not a string. It is also not necessary to remove characters so neither is converting the string to a list:

def isPalindrome(S):
    return len(S) < 2 or S[0] == S[-1] and isPalindrome(S[1:-1])

output:

print(isPalindrome("ABC"))    # False
print(isPalindrome("ABCBA"))  # True
print(isPalindrome("ABCCBA")) # True
print(isPalindrome("A"))      # True
Alain T.
  • 40,517
  • 4
  • 31
  • 51