I am trying to check for a palindrome. I keep getting errors when reaching the first if statement. I feel it is because i returns as a letter and not the position number in the string. What am I missing?

def palindrome(str):
  s=(str)
  sum=len(s)
  l=int(sum/2)
  for i in s[0:l]:
    if i == (len(s)-1-i):
      print "This word is a palindrome"
    else:
      print "This word is not a palindrome"

Recommended Answers

All 7 Replies

In your if statement (i) is a string and (len(s) - 1) gives an integer.
Just a note, don't use sum and str, those are in the Python syntax. Also l gets easily confused with the number 1.

Since you are already using slicing, why not this:

# a simple palindrome function

def palindrome(mystr):
    # test prints
    print mystr
    # this is mystr reversed via slicing
    print mystr[::-1]
    if mystr == mystr[::-1]:
        print "is a palindrome"
    else:
        print "is not a palindrome"

# test the function
mystr = "racecar"
palindrome(mystr)

print '-'*30

mystr = "racecars"
palindrome(mystr)

I would definately implement zztucker's idea. The syntax to reverse a list is:

a='hi there'
print a[::-1]
'ereht ih'

In case that was not clear.

Or shorten ZZucker function down to this.
All you need to know if it`s True or False.

def palindrome(mystr):
    return mystr.lower() == mystr[::-1].lower()

mystr = "racecar"
print palindrome(mystr)

Wouldn't it be easier just to test if the first letter is equal to the last letter?

Wouldn't it be easier just to test if the first letter is equal to the last letter?

That would be a good idea if the user expected many of the entries were not palindrones. That would possible save a fraction of time; however, it would only catch non-palindrones. Further methods would be implemented to see if it were indeed a palindrone. Have the same first and last letter doesn't guarantee a palindrone.

For example "spoons" has the same first and last letter.

The [::-1] function is already pretty quick/optimized so I'd just use that, or the other suggestion snippsat said.

def palindrome():
  s = requestString("Please enter your potential palindrome: ")
  for i in [(len(s)/2)]:
    if i == (len(s)-i-1):
     print "This word is a palindrome"
    else:
      print "This word is NOT a palindrome"

ZZucker your idea is simple and works. I also thought reversing the word and testing its equality to the original was the best way to go. However, my professor said "this is not a good way." He wants us to use a loop to compare each letter using his hint, which is line 4 from the code to follow--the code is mine. It works for odd numbered words, but I cannot get it to work on evens like deed. I get that a word like deed would bring one to position 2, which is the third value using his idea. Frustrating

Announce not anagram and break out of loop or return False, otherwise announce 'is anagram' or return True after the loop. Middle letter does not matter as it can be anything. Do not use l for variable name as it looks like I or 1. Line 2 should be s=str(s) and paramer should be s, str is built in type (class), to allow input to be also number. For example s[0] should equal s[-1].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.