-2

For some reason it works great but on some sentences that are palindrome it says they are not

palindrome = input("Enter a word: ")
palindrome = palindrome.lower()
palindrome.replace(" ", "")
if palindrome == palindrome[::-1]:
    print("OK")
else:
    print("NOT")

An example:"Mr Owl ate my metal worm" but on other sentences it works good and i don't understand whats different please help me btw the level of the code needs to be at this level

dEBA M
  • 457
  • 5
  • 19
binantwi
  • 1
  • 3

1 Answers1

1

instead of using replace whitespace ( if they are not require much ) then, you can convert the word to list of words sperated on whitespace and then create a new word joining all word inside the list and reverse it to check if the sentence is palindrome or not

here is code

palindrome = input("enter the word ")
palindrome = ''.join(palindrome.split()).lower()

if palindrome == palindrome[::-1]:
    print("OK")
else:
    print("NOT")

without using join

palindrome = input("enter the word ")
new_palin = ''
for chars in palindrome:
    if chars != ' ' :
        new_palin+=chars

new_palin = new_palin.lower()
if new_palin == new_palin[::-1]:
    print("OK")

else:
    print("NOT")
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • i can but the problem isn't in the replace. the replace works and it works with other sentences also cant use the join command so the replace is the only way i have to get rid of the spaces but i don't think that the problem is in the spaces anyway. – binantwi Mar 10 '20 at 06:01
  • ok , just updated solution if you can use for implemetation of replace – sahasrara62 Mar 10 '20 at 06:52