I am getting invalid syntax while using else statement. If anyone can heLP me please help? It's a python code. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I am getting invalid syntax while using else statement. If anyone can heLP me please help? It's a python code.

X=2 if x>1: print("yes") else: Syntaxerror:Invalid syntax (after pressing enter)

14th May 2019, 10:46 AM
Amisha
Amisha - avatar
8 Answers
+ 1
Else expects an non empty code block.
14th May 2019, 10:58 AM
Daniel Adam
Daniel Adam - avatar
+ 1
x = 2 if x > 1: print("yes") else: print("no") If you don't want anything to happen if x is not over 1, then don't write an else statement :)
14th May 2019, 11:09 AM
inxanedev!
inxanedev! - avatar
+ 1
Why you left else empty Line 5 else: print("no") Try this it will help
20th May 2019, 2:02 PM
twins gaming/hack
twins gaming/hack - avatar
0
Hi. The code somehow appears to be python? Well in this case the reason lies in the empty else block. There is nothing to be done and therefore an error is thrown. Put in a "pass" that does exactly nothing but makes the code valid. Be aware of case sensitivities. x = 2 if x > 1: print("yes") else: pass Cheers.
14th May 2019, 10:52 AM
ChrA
ChrA - avatar
0
Where I have to put a "pass"? Please tell me.
14th May 2019, 10:55 AM
Amisha
Amisha - avatar
0
Can you give a example with a code?
14th May 2019, 10:59 AM
Amisha
Amisha - avatar
0
You declered variable 'X' but used in if statement
19th May 2019, 11:33 AM
Karthik here
Karthik here - avatar
0
Variable X and x are different so there is error in program
21st May 2019, 12:49 PM
kanav