Abhijeet-AR
Abhijeet-AR

Reputation: 53

Palindrome logic in python: What is wrong with this program?

def isPalindrome(word):

    l = len(word)

    for i in range(l/2):
        if(word[i] != word[i+l-1]):
            return 0

    return 1

def main():

    print("\n\n\tTo Check if the word is a Palindrome\n\n")

    word = raw_input("Enter a word : ")

    if(isPalindrome(word) == 1):
        print("It is a Palindrome")

    elif:
        print("It is not a Palindrome")

main()

In my opinion everything is right in the program. It goes good when I enter a word which is not a palindrome but when I enter a palindrome it gives an error like this:

Enter a word : madam
Traceback (most recent call last):
  File "temp.py", line 16, in <module>
  File "temp.py", line 6, in isPalindrome
IndexError: string index out of range

Upvotes: 2

Views: 160

Answers (4)

pavikirthi
pavikirthi

Reputation: 1655

You should round the l/2 value

def isPalindrome(word):
    l = len(word)
    for i in range(round(l/2)):
        if(word[i] != word[i+l-1]):
            return 0
        return 1

print("\n\n\tTo Check if the word is a Palindrome\n\n")

word = input("Enter a word : ")
if(isPalindrome(word) == 1):
    print("It is a Palindrome")
else:
    print("It is not a Palindrome")

Upvotes: 1

Raymond Hettinger
Raymond Hettinger

Reputation: 226694

Change word[i+l-1] to word[l-i-1]:

def isPalindrome(word):

    l = len(word)

    for i in range(l // 2):
        if(word[i] != word[l-i-1]):
            return 0

    return 1

The goal is to get the word[l-i-1 to count down while i is counting up; hence, you need to subtract i rather than add it.

Also, I would change the l/2 to l // 2 so that it works in Python 3 as well.

Hope that helps :-)

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53565

First thing that is wrong is: elif: - if you're using else-if you should provide a condition, the fix in this case it to change it to else:

Second, the if should be: if(word[i] != word[l-i-1]): in order for the function to work (check that each letter is equal to its equivalent in the word).

Third, less critical but still important: keep the styling:

  1. remove redundant braces
  2. use proper naming convention (snake-case - not camel-case)
  3. use True/False as return values instead of 1/0
  4. use floor division // (as AChampion mentioned in the comments)

Complete code (fixed):

def is_palindrome(word):
    l = len(word)
    for i in range(l//2):
        if word[i] != word[l-i-1]:
            return False
    return True


def main():
    print("\n\n\tTo Check if the word is a Palindrome\n\n")
    word = raw_input("Enter a word : ")
    if is_palindrome(word):
        print("It is a Palindrome")
    else:
        print("It is not a Palindrome")


if __name__ == "__main__":
    main()

Upvotes: 3

Debanik Dawn
Debanik Dawn

Reputation: 799

Your logic for checking palindrome should be:

if(word[i] != word[l-1-i]):
     return 0

It's okay to do l/2 if you're on python 2 but python 3 will produce the result as a floating point value. Your code seems to be in py3.

And you need to give a condition to the elif block. Otherwise, change it to else.

Upvotes: 2

Related Questions