Skip to content
Advertisement

Creating a palindrome checker with Python’s loop and if statements

I am trying to follow the instructions to create a palindrome. I am given half a function, and I have to fill in the blanks. I am currently unable to get the loop to work correctly. I am also unsure how to add characters to the beginning or the end of string without using the + or a comma. I do not think that is what I am being asked to do. Here are the instructions;

The is_palindrome function checks if a string is a palindrome… Fill in the blanks in this function to return True if the passed string is a palindrome, False if not.

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = input_string.replace(" ", "")
    reverse_string = input_string.replace(" ", "")
    # Traverse through each letter of the input string
    for word in input_string: # Originally, I was only given the a FOR statement here, I wrote in the rest
        new_string+=word.replace(" ","").upper()
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 

    if ___:
        new_string = ___
        reverse_string = ___
    # # Compare the strings
      if ___:
          return True
          return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True

I have removed the empty spaces and made everything the same case. I have assigned the characters to new_string, but it looks like I am supposed to use join to add the characters, but when I do the print statement does not print anything. I am unsure how to add the items in reverse order. I am not even sure if I am on the correct track, because I am unsure what the IF statement is asking. I would think I should be able to use a loop to create the string and then compare the two strings.

Also, could someone please explain why new_string.join(word) does not print anything out? How am I using it incorrectly?

Thank you very much for any possible assistance.

Advertisement

Answer

It worked for me, I have tried and change little bit from above my code it work now with below code.

You can see it pass in their test and code verification, refer below screenshots and code.

enter image description here

def is_palindrome(input_string):
    # We'll create two strings, to compare them
    new_string = ""
    reverse_string = ""
    # Traverse through each letter of the input string
    for letter in input_string.strip():
        # Add any non-blank letters to the 
        # end of one string, and to the front
        # of the other string. 
        new_string = new_string+letter.replace(" ","")
        reverse_string = letter.replace(" ","")+reverse_string
    # Compare the strings
    if new_string.lower() == reverse_string.lower():
        return True
    return False

print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement