Why am I getting: SyntaxError: invalid syntax (<string>, line 1) in python?

Viewed 58

I'm doing an exercise to learn python and the question is simple, just to generate code that can print out if variable x is even or odd. I'm getting the correct output when I run the code, but I also receive the following error message:

SyntaxError: invalid syntax (, line 1)

Here's my chunk:

x = 1
if (x % 2) == 0:
  print("EVEN")

else:
  if(x % 2) != 0:
    print("ODD")

Can anyone tell me why I'm getting the error message along with the correct output?

Thanks!

1 Answers

More precise followup to my previous comment.

To the terminal, a blank line is interpreted as "the input is finished; go ahead an evaluate it".

When the evaluator saw the blank line, it assumed that your if statement was finished. It saw that your condition was False and did nothing.

Then it saw a raw else:. Whoa. That's a syntax error.

Then it saw the second if statement, and happily evaluated it and printed ODD'.

Related