Writing if statement in one line [on hold]

  • A+
Category:Languages

While the following statement is correct:

print( "10 is greater than 5" ) if 10>5 else print( "nothing here " ) 

Removing the else cause errors SyntaxError: unexpected EOF while parsing

print( "10 is greater than 5" ) if 10>5 

Why does this happen?

 


print is a function, and it takes a single value. In other words, you need a single call to print, and represent the value it takes as a onliner if:

print ("10 is greater than 5" if 10 > 5 else "nothing here") 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: