-1
def reverse(string):
    return string[::-1]
def isPalindrome(string):
    temp=reverse(string)
    if temp==string:
        return True
    else:
        return False

string='tanmay' # input('enter a word')
ans=isPalindrome(string)
if ans==1:
    print' Yes palindrome'
else:
    print' no its not a palindrome'

if I ask for an input from the user the error what I got was Traceback (most recent call last):

File "C:/Python27/prac06-2.py", line 10, in <module>
    string=input('enter a word')
  File "<string>", line 1, in <module>

NameError: name 'tanmay' is not defined

but when I enter a string by myself the program is executed successfully

  • 1
    You should first make sure that the code you post here is the same as the code you run. Judging by your error messages, it isn't. `False` with an upper `F` is what you should be using. – cs95 Nov 06 '17 at 15:49

2 Answers2

1

in python 2.7 input() evaluates the given input, you should use raw_input() to read in the data as a string. On another note, temp==string evaluates to a boolean so you do not need to put it in an if statement you can simply return temp==string

def reverse(string):
    return string[::-1]
def isPalindrome(string):
    temp=reverse(string)
    return temp==string

string=raw_input('enter a word')
if isPalindrome(string):
    print(' Yes palindrome')
else:
    print(' no its not a palindrome')

You can simplify isPalindrome() further by removing reverse() to:

def isPalindrome(string):
    return string == string[::-1]
ktzr
  • 1,625
  • 1
  • 11
  • 25
0

You are returning a boolean True or False and you are trying to compare the result with a value 1. Here is how you should invoke it.

ans = isPalindrome(string)
if ans: # you can also do (if ans == True)
    print 'Yes, it is a palindrome'
else:
    print 'No, it is not a palindrome'
Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36