athira babu athira babu - 1 month ago 16
Python Question

Returning result of palindrome program using while-loop

n = str(input('Enter the string:'))
def palindrome(n):
index = 0
check = True
while index<int(len(n)/2):
if n[index]==n[-1-index]:
index+=1
return True
break
return False
if palindrome(n)==True:
print('palindrome')
else:
print('not palindrome')


I am not getting the correct answer for this palindrome program.

Answer

The classic solution:

def palindrome(n):
    start = 0
    end = len(n) - 1
    while (start < end):
        if (n[start] != n[end]):
            return False
        start = start + 1
        end = end - 1
    return True
Comments