0

I'm making a basic Palindrome Checker in python for a small project. I have searched around and found no answer to this.

I am using the following code near the end of the script:

if String1 == String2:
               print("Yes")
else:
               print("No")

I receive a syntax error when I run this code. String1 is the text entered by the user and String2 is the text in reverse, which I develop earlier. I am running Python 3.2.3

Thanks in advance

I am using String2 == String1[::-1] for my Palindrome check and the error I receive is SyntaxError: invalid syntax

Edit: This is my exact code however I'm not exactly sure where to put else: I have tried it multiple times on both new lines and on the line before with no success. String1 in this instance is "racecar"

Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import easygui as eg
>>> String1 = eg.enterbox(msg="Enter a Word")
>>> String2 = String1[::-1]
>>> if String1 == String2:
    print("Yes")


Yes
>>> else:

SyntaxError: invalid syntax
>>> 
8
  • 7
    The code as posted (while using odd indentation) is valid. Can you post the actual traceback? Also, quick palindrome checking in Python: String1 == String1[::-1]. Commented May 8, 2012 at 19:06
  • 2
    Please edit your question and add the error that you get. The mistake is probably in how you reverse the string, show us that part too. Commented May 8, 2012 at 19:07
  • Not a python programmer, so this may be a dumb answer, but python enforces pretty strict rules on whitespace, doesn't it? Maybe thats the issue? Commented May 8, 2012 at 19:19
  • Please paste the actual traceback you receive. There's something you're not providing, because what you've shown us does not demonstrate a syntax error. Also, see my answer. Commented May 8, 2012 at 19:20
  • Sometimes the error occurs in this line if you failed to close a bracket ( somewhere above. Commented May 8, 2012 at 19:22

5 Answers 5

4

Using [::-1] to check for palindromes should work like a charm:

>>> s1 = "This is a string."
>>> s2 = "racecar"
>>> s1 == s1[::-1]
False
>>> s2 == s2[::-1]
True

There's something in your code that you're not showing us. You need to paste a larger code snippet, or the actual traceback you receive.

And, now to account for your pasted traceback - you did not indent the print command after the if statement. Because the if clause ended immediately it makes no sense (syntax error) to provide an else. Indentation is syntax in Python. You need it to look like this:

if String1 == String1[::-1]:
  print("Yes")
else:
  print("No")

The spacing is mandatory.

Sign up to request clarification or add additional context in comments.

Comments

2
print(["No", "Yes"][string1 == string1[::-1]])

Comments

2

Your basic problem is that you're trying to do all this at the interactive prompt. You can do it, but you need to pay extra attention to make sure Python interprets all your statements as part of the same block. Blank lines will be significant here; since you are putting a blank line before else:, Python thinks that you have ended your if: and that you're trying to use else: at the beginning of a block, and this is indeed a syntax error.

So until you understand the ropes, it's going to be a lot simpler if you just put your code in a file and run it from there. You can do this with IDLE (File > New, then F5 to run your code) or you can use about any text editor and run your code from a command window.

Comments

0

After the line

if String1 == String2:
    print("Yes")

press Enter, then press Backspace, and just then write:

else:

press Enter agian, and write:

print("no")

Comments

0

You've already accepted an answer, but I'd have written that like:

print 'Yes' if String1 == String2 else 'No'

which encapsulates a single concept (selecting between two possible answers) into a single line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.