It reads the same forwards and backwards.

★★★

A palindrome is a word, number or phrase that reads the same backwards as forwards, such the words madam or racecar.

Palindrome

Make

Write a program that allows the user to enter a word or sentence. The program outputs whether the input is a palindrome or not.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called palindrome that:

  1. Takes a string parameter phrase to be processed.
  2. Returns True if phrase is a palindrome.
  3. Returns False if phrase is not a palindrome.

Note the algorithm will need to ignore case, whitespace and punctuation.

Complete the main program so that:

  1. The user can input a word, phrase or sequence of digits.
  2. The program outputs whether the phrase is a palindrome or not.

Typical inputs and outputs from the program would be:

Enter a phrase: ABBA
ABBA is a palindrome.
Enter a phrase: 02/02/2020
02/02/2020 is a palindrome.
Enter a phrase: A man, a plan, a canal – Panama
A man, a plan, a canal – Panama is a palindrome.
Enter a phrase: Hello World
Hello World is not a palindrome.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
# Palindrome program

# -------------------------
# Subprograms
# -------------------------
---
def palindrome(phrase):
---
    # Convert to lowercase for like-for-like comparisons
    phrase = phrase.lower()
    is_palindrome = True
    # Set indexes to beginning and end of the phrase
    letter_front = 0
    letter_back = len(phrase) - 1
---
    
    # Check all the letters up to half way or if the phrase is not a palindrome
    while (letter_front < len(phrase) // 2) and is_palindrome:
---
        # Skip over any characters that are not alphanumerics
        while not phrase[letter_front].isalnum():
---
            letter_front = letter_front + 1
---
        while not phrase[letter_back].isalnum():
---
            letter_back = letter_back - 1
---
        # If the two letters are not the same the phrase is not a palindrome
        if phrase[letter_front] != phrase[letter_back]:
---
            is_palindrome = False
---
        # Increment and decrement the indexes
        letter_front = letter_front + 1
        letter_back = letter_back - 1
---
    return is_palindrome
---


# -------------------------
# Main program
# -------------------------
---
phrase = input("Enter a phrase: ")
---
# Check if the phrase is a palindrome
if palindrome(phrase):
---
    print(phrase, "is a palindrome.")
---
else:
---
    print(phrase, "is not a palindrome.")