Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I to generate a program that will verify if a word is a palindrome or not. I can able to generate that code and it is working with words that have an even quantity of numbers. I want guidance on how to find out if a number is odd. Is there any simple way to find if a number is odd or even?

My code:

a = 0

while a == 0:

    print("\n \n" * 100)

    print("Please enter a word to check if it is a palindrome: ")

    word = input("?: ")

    wordLength = int(len(word))

    finalWordLength = int(wordLength / 2)

    firstHalf = word[:finalWordLength]

    secondHalf = word[finalWordLength + 1:]

    secondHalf = secondHalf[::-1]

    print(firstHalf)

    print(secondHalf)

    if firstHalf == secondHalf:

        print("This is a palindrom")

    else:

        print("This is not a palindrom")

    print("Press enter to restart")

    input()

1 Answer

0 votes
by (108k points)

A simple solution is to check if the number is even or not. For that, you can use the % sign, which is just like division and it will calculate the remainder, so if the number divided by 2 has a remainder of 0. 

if num % 2 == 0:

    pass # Even 

else:

    pass # Odd

Or you can also use this:

if num % 2:

    pass # Odd

else:

    pass # Even 

Looking for a Python Tutorial? Join the Intellipaat's Python Course to gain more knowledge on Python. 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 7, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 7, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer

Browse Categories

...