-1

I am writing a function to check if a word is a palindrome

def palidrome(b):
    word = ''.join(reversed(b))
    if b == word:
        return True
    return False


def main():
    so = input("Please enter a matching word")
    come = palidrome(so)
    print(come)

main()

Whatever I put, e.g., 'mom,' 'dad' or 'racecar,' it always outputs False but it should be True.

Adil Ali
  • 7
  • 5
  • Your code is working fine just problem in input staement. I will also suggest you to store string in list and than start traversing it from left and right end simultaneously. And just check that their values are equal. This will reduce time of computation. – learner-coder Oct 08 '19 at 02:17
  • No directly for your question, however, it is very inefficient code... Isn't it better to compare a front half and a back half? – ghchoi Oct 08 '19 at 02:21

2 Answers2

1
 def checkPalindrome(word):
       wordCopy = word[::-1]
       if word == wordCopy:
         return True
       else:
         return False
  def main():
      s = 'oro'
      print(checkPalindrome(s))
  main()
Z. Cajurao
  • 357
  • 2
  • 12
0

According to this demo, your code is running fine - however, I noticed your input statement doesn't have a space after it. Are you typing a space before you put your word in? If so, consider the strip() function, which will remove leading and trailing spaces - or just add a space to your input prompt!

Nick Reed
  • 4,989
  • 4
  • 17
  • 37