+ 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
15 ответов
+ 10
Check indentation is it 4 spaces in IDE?
+ 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
+ 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
+ 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...
.....
+ 1
Should run properly.
+ 1
Do you indent else? That's a mistake because if is not indented.
+ 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
+ 1
HonFu thank you. checking now.
+ 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
0
I am getting (SyntaxError: invalid syntax) error after pressing Enter key after writing (else:) in the IDE
0
here is a copy paste:
>>> x = 4
>>> if x == 5:
print ("Yes")
else:
SyntaxError: invalid syntax
0
Can't help as I can't see what r u doing actually with IDLE. Can u share a screenshot if possible pls?
0
HonFu please show me how to indent. i am greateful
0
Seniru Pasan I dont think i can share screenshot here. right?
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