1

I'm trying to write a program that finds whether is a palindrome or not. I have this code and can't figure out for the life of me why it won't work. It runs but only returns that the number is not a palindrome regardless of whether it is or not

def reverse(number):
    reverse = 0    
    while number > 0:
        endDigit = number % 10
        reverse = (reverse*10) + endDigit
        number = number // 10

def isPalindrome(number):
    reverse(number)
    original = number    
    if original == reverse:
        print("yep, it's a Palindrome")
    else:
        print("sorry man, not a Palindrome...")

def main():
    number = eval(input("Please enter a number and I'll check if it's a Palindrome: "))
    isPalindrome(number)
main()
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
Casey Monson
  • 11
  • 1
  • 3
  • Your `reverse()` function calculates the reversed number in a local variable, which is not returned or otherwise made available outside of the function. There is no point in calling this function at all. – jasonharper Feb 26 '18 at 04:21
  • So how would I make it so that the reverse() function is made available in the isPalindrome() function? – Casey Monson Feb 26 '18 at 04:28
  • also I just realized that you're right, the calling of my reverse() function was completely pointless in the main() function haha – Casey Monson Feb 26 '18 at 04:33

2 Answers2

3

Never use eval. Never.

Firstly, let me explain your mistakes. Your expectations about this program is wrong. You probably want reverse(number) to reverse the number itself. E.g. after reverse(123), 123 should become 321. But, wait, 123 is just a number, like a number in math, you know. Do you really want to 123 to become 321? Can you predict what will happen after that? You will now pay 321 for a rent instead of 123, because you have changed the number itself! Though, these are actually good news, because I have exactly $123 on my bank account, and this is probably the easiest way to increase my money.

Well, this is just a joke. But the point is: you cannot change a number passed to the function. This has been done for the security reasons. There are mutable and immutable objects in python. Number is immutable. You cannot change a number, you can only assign another number:

a = 123
a = 312 # 123 is still 123, 321 is still 312, but a is 321 now

Lists are mutable objects, e.g:

a = []
a.append(1) # I can change the list itself, whoa!
a.append(2) # one more time
a = [3] # and even assign a new list

Finally, the main point is: you cannot change passed number to a function. But: you can return a new number! This is the point - just return a result from your reverse function and assign it somewhere like that:

def reverse(number):
    ...
    return result

r = reverse(123)

reverse wont change 123, it wont break the world, it just returns a new number, and everything works fine.

By the way, there is a simpler way to check a palindrome:

In [5]: def is_palindrome(s):
   ...:     return s == s[::-1]
   ...: 

In [6]: n = input('Enter number: ')
Enter number: 123321

In [7]: if is_palindrome(n):
   ...:     print('You cool')
   ...: else:
   ...:     print('No luck')
   ...:     
You cool

s[::-1] means `take each symbol from the start till the end in a reverse order. This is a slice

awesoon
  • 32,469
  • 11
  • 74
  • 99
0

Alternatively, have you considered representing the integer as a string and comparing it to its reversed form?

In [1]: num1 = 907709

In [2]: num2 = 90789

In [3]: str(num1) == str(num1)[::-1]
Out[3]: True

In [4]: str(num2) == str(num2)[::-1]
Out[4]: False

Addressing your current code: you need to return something from reverse(), and assign something to that returned value in isPalindrome().

def reverse(number):
    rev = 0    
    while number > 0:
        endDigit = number % 10
        rev = (rev*10) + endDigit
        number = number // 10
    return rev

def isPalindrome(number):
    rev = reverse(number) 
    if number == rev:
        print("yep, it's a Palindrome")
    else:
        print("sorry man, not a Palindrome...")

That said, reverse() will still fail here. Consider:

In [28]: reverse(10750)
Out[28]: 5701

If you don't consider this valid, you should use the string form of the integer (str(number)), which will retain trailing zeros when reversing the number. Using [::-1] is a form of indexing to reverse a string.

Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
  • Is there possibly a way to make some sort of an edit to my code and make it do what it's intended to? Just a way to make my reverse() function available to be used inside of the isPalindrome() function – Casey Monson Feb 26 '18 at 04:38
  • Okay I see, so it is completely necessary to change it to a string in order to find the palindromes. Didn't think about the trailing zeros problem. – Casey Monson Feb 26 '18 at 05:14