Create a program, palindrome.py, that has a function that takes in one string argument and prints a sentence indicating if the text is a palindrome. The function should consider only the alphanumeric characters in the string, and not depend on capitalization, punctuation, or whitespace. If the string is a palindrome, it should print: It's a palindrome! However, if the string is not a palindrome, it should print: It's not a palindrome!
The Problem
My code is not printing whether it is a palindrome when there are spaces inside the string, but does print that it is not a palindrome despite having spaces. I included replace(), zip(), and reversed() in my code to account for the spaces and the reversed words, but it is not printing the desired result.
What am I missing or doing wrong in my code?
import sys
def palindrome(words):
if ' ' in words:
palindromes = words[::-1]
if palindromes == words:
return "It's a palindrome!"
return "It's not a palindrome!"
print(palindrome(sys.argv[1]))
Example Test Cases
Test Case 1
tests 1 Run python3 palindrome.py 'Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned' and match its output to an expected value.
Test Case 2
test 2 Run python3 palindrome.py 'Ed, I saw Harpo Marx ram Oprah W. aside' and match its output to an expected value.
Outputs
Expected Output: "It's a palindrome!"
Actual Output: "It's not a palindrome!"