Not returning any values

Siva Kumar
def pal(n):
    if n[0]==n[len(n)-1] and len(n)!=1:
        pal(n[1:len(n)-1])
    elif(len(n)==1):
        return True
    else: 
        print("Not a Palindrome")
if pal("madam"):
    print("Yes, It is a Palindrome")
   

I have tried to return value as "True" to print if statement. what is the issue with the program?

DrummerMann

You could do something like this, you need to return your recursive function. There is also a little mistake, you check for len == 1, this won't always work when it has an even number of characters.

def pal(n):
    if len(n) <= 1:
        return True

    if n[0] == n[-1]:
        return pal(n[1:-1])
    
    return False

if pal("madam"):
    print("Yes, It is a Palindrome")
else:
    print("No, It is not a Palindrome")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related