-5

When the number is palindrome instead of returning True the code returns None.

def is_palindrome(n):
    string = str(n)
    length = len(string)

    if length%2 == 0:
        for k in range(0, int(length/2)):
            if string[k] == string[length-k-1]:
                continue
                return True
            else:
                return False

    if length%2 != 0:
        for k in range(0, int(length/2)):
            if string[k] == string[length-k-1]:
                continue
                return True
            else:
                return False

print(is_palindrome(1221))
Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • 3
    `return True` is never reached because of the `continue`. You also probably want to `return True` at the end of the loop if you haven't already returned `False`. – deceze Mar 23 '21 at 20:32
  • 1
    What is `continue; return True` supposed to mean? `continue` immediately jumps to the beginning of the innermost loop, and control never reaches `return True` – ForceBru Mar 23 '21 at 20:32
  • Learn to use a debugger: https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – Lesiak Mar 23 '21 at 20:36

1 Answers1

2

I see how you're trying to do it, and I don't know if you want me to keep the methodology of your solution, but here is a simpler way that you can solve this problem.

def pal(x):
    if str(x) == (str(x)[::-1]):
        return True
    else:
        return False

This function just checks if the number given is the same reversed. Hope this helps. With your method, the problem probably has to do with your use of 'continue', which I personally don't use much although I know it could prove to be quite useful.

fusunnn
  • 118
  • 7