I am a beingger for using Python,now I am doing a palindrome program,but I have tried many time it still doenst work, can anyone help me to fix it??Thanks a lot!!!!!


Here is the code:

s=input("Enter alphabetic characters: ")
def palindrome(s):
    index=0
    c=True
    while index<len(s):
        if s[index]==s[-1-index]:
            index +=1
            return True
        return False
if palindrome(s)==True:
    print ("This is alphabetic characters")
else:
    print ("This is not alphabetic characters")

you should check only half way of the word discarding non-alphabet letters in multiword case. Your print should say palindrome. There is also simple way of checking if word is equal to it reversed.

I am not quite sure how to fix it....
can you tell me more details?
Thank you~

We usually get the palindrome homework questions earlier in the year. To solve your problem, add some print statements at the very least:

s=input("Enter alphabetic characters: ")
def palindrome(s):
    index=0
    c=True
    while index<len(s):
        print "comparing", s[index], s[-1-index], "for index", index
        if s[index]==s[-1-index]:
            index +=1
            print "returning True"
            return True
        print "returning False"
        return False
if palindrome(s)==True:
    print ("This is alphabetic characters")
else:
    print ("This is not alphabetic characters")

We usually get the palindrome homework questions earlier in the year. To solve your problem, add some print statements at the very least:

s=input("Enter alphabetic characters: ")
def palindrome(s):
    index=0
    c=True
    while index<len(s):
        print "comparing", s[index], s[-1-index], "for index", index
        if s[index]==s[-1-index]:
            index +=1
            print "returning True"
            return True
        print "returning False"
        return False
if palindrome(s)==True:
    print ("This is alphabetic characters")
else:
    print ("This is not alphabetic characters")

Thank you~~but sometimes it doesnt work~~such as:"afhhjkloia" it will show it is palindrome

you should check only half way of the word discarding non-alphabet letters in multiword case. Your print should say palindrome. There is also simple way of checking if word is equal to it reversed.

I am not quite sure how to fix it....
can you tell me more details?
Thank you~

At least read the output printed by your script ! woooee has been nice enough to add traces to your script, don't expect s/he isn't going to DO IT for you.
HINT: How many times do you expect " print "comparing", s[index], s[-1-index], "for index", index " to be executed and check how many time is it actually executed.

At least read the output printed by your script ! woooee has been nice enough to add traces to your script, don't expect s/he isn't going to DO IT for you.
HINT: How many times do you expect " print "comparing", s[index], s[-1-index], "for index", index " to be executed and check how many time is it actually executed.

Thank you~~~ I am trying it,hope it can work~

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.