1

The code is getting compiled successfully but no message is being displayed, i.e no print command is being executed.

What's the error in my Python 3.7 code?

def isPalindrome(n):
    s=0
    while n!=0 :
        d=n%10
        s+=d
        s*=10
        n/=10
    if n==s :
        return True
    else :
        return False

def main():
    if isPalindrome(252) :
        print('252 is a Palindrome Number')
    else :
        print('252 is not a Palindrome number')
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 1
    `main` will not execute when you run the program. Add `if __name__ == "__main__": main()` to the bottom of your program. – modesitt Apr 17 '20 at 20:34
  • 2
    Does this answer your question? [What does if \_\_name\_\_ == "\_\_main\_\_": do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do) – modesitt Apr 17 '20 at 20:35

3 Answers3

1

main doesn't execute on it's own in Python.

You can put it in:

if __name__ == "__main__":
    main()

or you can just put main() outside entirely, like so...

def isPalindrome(n):
    s=0
    while n!=0 :
            d=n%10
            s+=d
            s*=10
            n/=10
    if n==s :
            return True
    else :
            return False

def main():
    if isPalindrome(252) :
            print('252 is a Palindrome Number')
    else :
            print('252 is not a Palindrome number')

main()

the former is better if you want to import the function into another program.

But the latter is fine if it's just a stand alone program

mark pedersen
  • 245
  • 1
  • 9
0

Add this

if __name__ == "__main__":
    main()
yudhiesh
  • 6,383
  • 3
  • 16
  • 49
0

You don't need to define main here. Below will do for you .

def isPalindrome(n):
        s=0
        while n!=0 :
                d=n%10
                s+=d
                s*=10
                n/=10
        if n==s :
                return True
        else :
                return False


if isPalindrome(252) :
        print('252 is a Palindrome Number')
else :
        print('252 is not a Palindrome number')
lostin
  • 720
  • 4
  • 10