Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

This is a simple palindrome checker. The code works for numbers, but not strings.

x = str(input("Enter a number: "))
if x == x[::-1]:
    print x + " is a palindrome!"
else:
    print x + " is not a palindrome!"

When I try inputting a string, I get this error:

  File "palindrome.py", line 1, in <module>
    x = str(input("Enter a number: "))
  File "<string>", line 1, in <module>
NameError: name 'abba' is not defined
share|improve this question

It seems you are using Python 2.x!

Please use raw_input for keyboard input. This always returns a string, so there is no need to cast/convert.

In Python 2.x input tries to evaluate the text entered as Python, and you never defined abba, so it will cause a NameError

See docs for more.

share|improve this answer
1  
And the str() can be dropped; raw_input() returns a string, always. – Martijn Pieters Nov 18 '13 at 9:58

Where you input some numbers.For example,"Enter a number: 1221".you can get

x=str(1221)

it's rigth.

But, "Enter a number: abba".you can get

x=str(abba)

it's wrong.Beacuse the "abba" is not string, and is not defined.

you can try "Enter a number: 'abba'", or use

raw_input('Enter a number:')

replace input("Enter a number: ") and You can get the results you want.

read more information on here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.