+ 2

else code not working

Below code is not working in python3 IDLE: x = 4 if x == 5: print("Yes") else: print("No") Result: SyntaxError: invalid syntax

14th Feb 2019, 10:52 AM
Hussain Siddique
Hussain Siddique  - avatar
15 ответов
+ 10
Check indentation is it 4 spaces in IDE?
14th Feb 2019, 10:56 AM
Hubert Dudek
Hubert Dudek - avatar
+ 3
The code is perfectly valid and doesn't raise an error neither in Sololearn nor when I copy/paste it in IDLE. The amount of spaces doesn't matter. When the SyntaxError is displayed, which position in which line does the interpreter point to? It should look something like this: if x == y ^ SyntaxError: invalid syntax
14th Feb 2019, 11:09 AM
Anna
Anna - avatar
+ 2
Your code is working as it was expected As u have mentioned IDLE, the case would be that you haven't press tab (or indent the code) after the '>>>' Make sure to indent properly in IDLE
14th Feb 2019, 11:10 AM
Seniru
Seniru - avatar
+ 2
Have you written anything below your else? It can't stand without anything. Make sure to write like this: if... ... else... ... Not like this: if... .... else... .....
16th Feb 2019, 9:16 AM
HonFu
HonFu - avatar
+ 1
Should run properly.
14th Feb 2019, 11:09 AM
HonFu
HonFu - avatar
+ 1
Do you indent else? That's a mistake because if is not indented.
14th Feb 2019, 1:40 PM
HonFu
HonFu - avatar
+ 1
Hussain Siddique, indentation is essential in Python, you really need to understand and master it. Please check out this: https://www.python-course.eu/python3_blocks.php
14th Feb 2019, 5:10 PM
HonFu
HonFu - avatar
+ 1
HonFu thank you. checking now.
14th Feb 2019, 5:11 PM
Hussain Siddique
Hussain Siddique  - avatar
+ 1
Hussain Siddique If you use the keyword "else", python will expect an else statement (or a block of code) to be executed if the condition is False: if condition: do_something() else: do_something_else() If you want to use an "empty" else clause, you can use the keyword "pass": if condition: do_something() else: pass # don't do anything This is equivalent to an empty pair of curly braces in other languages: if(condition) do_something(); else {} // don't do anything if condition is false However, if there is nothing to do in case the condition is False, you usually wouldn't use an else clause at all: if condition: do_something() # no else clause needed
16th Feb 2019, 9:58 AM
Anna
Anna - avatar
0
I am getting (SyntaxError: invalid syntax) error after pressing Enter key after writing (else:) in the IDE
14th Feb 2019, 1:37 PM
Hussain Siddique
Hussain Siddique  - avatar
0
here is a copy paste: >>> x = 4 >>> if x == 5: print ("Yes") else: SyntaxError: invalid syntax
14th Feb 2019, 1:38 PM
Hussain Siddique
Hussain Siddique  - avatar
0
Can't help as I can't see what r u doing actually with IDLE. Can u share a screenshot if possible pls?
14th Feb 2019, 1:39 PM
Seniru
Seniru - avatar
0
HonFu please show me how to indent. i am greateful
14th Feb 2019, 5:07 PM
Hussain Siddique
Hussain Siddique  - avatar
0
Seniru Pasan I dont think i can share screenshot here. right?
14th Feb 2019, 5:07 PM
Hussain Siddique
Hussain Siddique  - avatar
0
The code worked well in pycharm. But the problem is it's not working in python IDLE. Below is the copy of where I am getting error. I tried many ways to indent >>> if x == 4: print ("NO") else: SyntaxError: invalid syntax
16th Feb 2019, 9:14 AM
Hussain Siddique
Hussain Siddique  - avatar