I need help with my Palindrome code
For some reason, I cannot seem to convert the text into lower case letters and I cannot seem to ignore spaces and commas and periods and so on.

Here is my code

def is_palindrome(user_input):
    i = 0
    result = True
    while i <= len(user_input) -1 and result == True:
        while result == True:
            user_input = [user_input.lower() and user_input.isalpha()]
                
        if user_input[i] == user_input[-1 - i]:
            result =  True
        else:
            result = False
        i += 1
    return result
Member Avatar for masterofpuppets

hi
try this:

def remove_non_words( s ):
    new_s = ""
    for l in s.lower():
        if l in "abcdefghijklmnopqrstuvwxyz":
            new_s += l
            
    return new_s

def is_palindrome( user_input ):
    i = 0
    user_input = user_input.lower() #lowercase
    user_input = remove_non_words( user_input ) #remove punctuation and stuff...
    result = True
    while i <= len( user_input ) - 1 and result == True: #remove second while loop...
        if user_input[i] == user_input[-1 - i]:
            result =  True
        else:
            result = False
        i += 1
    return result

print is_palindrome( "racecar" )

hope this helps :)

commented: Thanks +0

Thanks for your help.
P.S. Your comments on the lines very nice, I get how to use those methods now

And a shorter version that uses the reverse string metod in a smart way.

>>> def is_palindrome(s): 
...     return s.lower() == s[::-1].lower()
... 
>>> is_palindrome('test')
False
>>> is_palindrome('racecar')
True
>>> is_palindrome('RACECAR')
True

Oh, that way is pretty nice, so short compared to original, but uses different to express similar logic

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.