Click here to Skip to main content
14,732,702 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a code which is giving output for string input but not for integer input.

What I have tried:

def check_palindrome(string):
    length = len(string)
    first = 0 #first character
    last = length -1 #last character
    status = 1
           if(string[first]==string[last]): #checking if the increment and decrement
                                            #of the first and last characters results in equal character
               first=first+1 #incrementing first character by 1
               last=last-1 # decrementing last character by 1
               status = 0
    return int(status) #true or false
string = input("Enter the string: ")

status= check_palindrome(string)
    print("It is a palindrome ")
    print("Not a palindrome")
Updated 26-May-20 5:29am

Simply transform the integer into its string representation before passing it to your function. Palindromes are always string related.
result = check_palindrome(str(intValue))
Member 14842958 28-May-20 5:50am
It's not working when I am testing with 123454321 and 78493.
phil.o 28-May-20 6:05am
Then debug your code to see where is the issue.
The python debugger

I do not understand why you don't return True or False from your function. Python has a boolean type, so why no using it?
status = True  # instead of status = 1
status = False # instead of status = 0
return status  # instead of return (int)status
Member 14842958 28-May-20 6:31am
because its hard coding. Isn't it?
phil.o 28-May-20 6:48am
No, because there is no obvious reason for not using a boolean type when it actually exists :)
Two ways:
1) Write a separate method that takes an integer parameter and checks for palindrome integers by converting it to a string, and passing the string to your existing method. Quick and dirty, inefficient as heck, but hey! It works.
2) Write a separate method that takes an integer parameter, and does the same as your existing method, but extracting each digit using the BitwiseOperators - Python Wiki[^] - AND and SHIFT should do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900