0

So I am using 2 different functions to evaluate if an inputed number is a palindrome. But it seems like no matter what I do my result is always "The number is not a palinrome" Even if the number actually is. This is my code, it should be easy enough but it's just not working.

def reverse(number):
    return (int(str(number)[::-1]))
def isPalindrome(number):
    reverse(number)
    if number == reverse(number):
        return True
    else:
        return False
def main():
    num = (input("Enter a number: "))
    if isPalindrome(num) == True:
        print("The number is a Palindrome")
    elif isPalindrome(num) == False:
        print("The number is not a Palindrome")
main()
user2767528
  • 15
  • 1
  • 2
  • 7
  • Your code works for me. For '121' or '12321' give me "The number is a Palindrome" – furas Oct 25 '13 at 01:41
  • It worked for me when i *gave* it 112232211. – wwii Oct 25 '13 at 01:42
  • Your code works for me using Python2.7. This isn't your problem, but you don't need the first `reverse(number)`, and can just write `isPalindrome(num)` instead of `isPalindrome(num) == True` and `not isPalindrome(num` instead of `isPalindrome(num) == False`. – mdml Oct 25 '13 at 01:42
  • I've tried using all the things you suggested and it still doesn't work. I am using 3.3.2, so that may be why. I am used to 2.7 but the class I am doing this for requires I use 3.3 so it gets confusing sometimes. I don't see what else could be wrong. – user2767528 Oct 25 '13 at 01:47
  • Fixed it. I just add to make both numbers the same type (even though they are both ints) so adding int before (number and reverse(number)) worked like a charm. Thanks everyone for your help. – user2767528 Oct 25 '13 at 02:09
  • There are a lot of questions based around "I tried to compare a string to an int and it didn't work". For instance: http://stackoverflow.com/questions/3270680/how-does-python-compare-string-and-int – Dominique McDonnell Oct 25 '13 at 02:14

1 Answers1

2

In isPalindrome(number) number is string but reverse(number) return int

so number == reverse(number) is always False (string != int)

Change reverse()

def reverse(number):
    return number[::-1]

def isPalindrome(number):
    #print(type(number))
    #print(type(reverse(number)))
    return number == reverse(number)

def main():
    num = input("Enter a number: ")

    if isPalindrome(num) == True:
        print("The number is a Palindrome")
    else:
        print("The number is not a Palindrome")

main()

or use num = int(input("Enter a number: "))

def reverse(number):
    return int(str(number)[::-1])

def isPalindrome(number):
    #print(type(number))
    #print(type(reverse(number)))

    return number == reverse(number)

def main():
    num = int(input("Enter a number: "))

    if isPalindrome(num) == True:
        print("The number is a Palindrome")
    else:
        print("The number is not a Palindrome")

main()
furas
  • 134,197
  • 12
  • 106
  • 148