-2

What exactly is wrong with the syntax and why in the following piece of code? I've counted the parentheses among other things yet am unable to figure it out.

c = ""
 print("Yes") if c else print("No")

Note: It gives a Syntax error like the one below:

print("Yes") if c else print("No")
                            ^
SyntaxError: invalid syntax
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Apurva Kunkulol
  • 445
  • 4
  • 17

1 Answers1

4

This happens because the print function behaves differently in python2 and python3:
Meanwhile in python3 your code works perfectly, in python2 it raises an error.
This happens because in python2, print is actually a statement and not a function; here you can find a more in-depth QA on the difference between functions and statements.

By the way, you can solve your problem importing the python3 print function from the future:

from __future__ import print_function

c = ""

print("Yes") if c else print("No")

OUTPUT:

No
Gsk
  • 2,929
  • 5
  • 22
  • 29