简体   繁体   中英

Python Help, If and Else

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
>>> 

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 .

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

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.

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")

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM