0

So i was looking around on here and i found the code for a working palindrome

def isPalindrome(): string = input('Enter a string: ') string1 = string[::-1] if string[0] == string[(len(string)-1)] and string[1:(len(string)-2)] == string1[1:(len(string)-2)]: print('It is a palindrome') else: print('It is not a palindrome') isPalindrome() 

So i already changed input to raw_input. And it works.

But in the comments there was a simpler code:

def isPalindrome(): string1 = input('Enter a string: ') string2 = string[::-1] if string1 == string2: return 'It is a palindrome' return 'It is not a palindrome' isPalindrome() 

I got the read back:

Traceback (most recent call last): File "C:\Python27\idk1.py", line 8, in <module> isPalindrome() File "C:\Python27\idk1.py", line 2, in isPalindrome string1 = input('Enter a string: ') File "<string>", line 1, in <module> NameError: name 'racecar' is not defined 

So i changed it to raw_input, and I wouldn't work at all. I'm curious as to why that is?

3
  • What part of your code uses racecar? Commented Dec 2, 2013 at 3:09
  • Is this a question for your homework? Commented Dec 2, 2013 at 3:09
  • @fdsa: He clearly tried to enter the string "racecar" into a Python 2 input prompt. Commented Dec 2, 2013 at 3:13

3 Answers 3

5

You have a problem with copy-and-paste, my friend. You copied the source, changed variable names but forgot to check the variable use throughout the program:

def isPalindrome(): string1 = input('Enter a string: ') string2 = string1[::-1] #notice it's string1, not string. if string1 == string2: [code] 
Sign up to request clarification or add additional context in comments.

Comments

0
def isPalindrome(): string1 = raw_input('Enter a string: ') string2 = string1[::-1] if string1 == string2: return 'It is a palindrome' return 'It is not a palindrome' 

In line 3, it said string instead of string1.

1 Comment

I continuously make these silly mistakes, thank you!
0

This code is working for me... Sorry for the previous post...

def isPalindrome(): string1 = raw_input('Enter a string: ') string2 = string1[::-1] if string1 == string2: return 'It is a palindrome' return 'It is not a palindrome' print isPalindrome() 

Output:

Enter a string: madam It is a palindrome 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.