Search code examples
python-3.xfunctionpalindrome

def Palindrome() doesn't return Palyndrom


I face the problem and don't know the way to fix it. Code works good but anyway it reponds that 'message' isn't palyndrom even if it's really palindrom. But when I revise it with one word only it goes. Thanks!

import string


def MultiplePalyndrome():
    isPalyndrome = True
    message = input('Your message\n').lower()
    punct = set(string.punctuation)
    gap = set(string.whitespace)
    message_revis = ''.join(x for x in message if x not in punct or x not in gap)
    i = 0
    while i < len(message_revis) / 2 and isPalyndrome:
        if message_revis[i] != message_revis[len(message_revis) - i - 1]:
            isPalyndrome = False
        i = i + 1
    if isPalyndrome:
        print("It's palyndrome! Yeah.")
    else:
        print("Nope. It's NOT palyndrome!")


MultiplePalyndrome()

i really dont know what is the problem.


Solution

  • if x not in punct or x not in gap
    

    is always true since every char is not whitespace OR not punctuation. You probably want:

    if x not in punct and x not in gap
    

    This transforms via DeMorgan's Law to:

    if not (x in punct or x in gap)