Chirvin
Chirvin

Reputation: 13

Colon syntax error in if statement

Ok, so i'm pretty new to the whole coding scene, and i've been working on python through code academy. On the site there is an activity where you make a pig latin translator. I was able to make this just fine, but when I attempted to recreate it in the python shell I was getting some weird errors in the if statement.

pyg = "ay"

original = raw_input("Enter a word bro:")\
if len(original) > 0 and original.isalpha():\

    print original\


else:
    print "error"

I get a syntax error on the colon ending the statement, so I assumed it was some kind of indentation error. However when I put a line space between the line previous and the if statement, it works just fine! Can anyone explain why this is??

Upvotes: 1

Views: 444

Answers (1)

Aaron
Aaron

Reputation: 11075

Ending each line with \ is what's tripping you up. (See the documentation for "Explicit line joining")

\ is the chatacter used to continue a line if you want to have a "single line" span multiple lines of text

The python interpreter sees it and ignores the next newline character and thus sees:

pyg = "ay"

original = raw_input("Enter a word bro:")if len(original) > 0 and original.isalpha():
    print original

else:
    print "error"

when you add the extra space it works because the if statement is again on it's own line

Upvotes: 1

Related Questions