0

When ever i use else: and then i hit enter then syntax errors pops ip

If else

16th Apr 2019, 1:22 PM
Aman Agrawal
Aman Agrawal - avatar
20 Answers
0
Your code should fail in line 2 already, because you haven't defined a variable x, only X. It would be best if you upload your code in 'Code Playground' and link it here.
16th Apr 2019, 1:58 PM
HonFu
HonFu - avatar
+ 4
In the code you posted here, there is syntax errors when you write the start of a line with capitals, but I suppose your smartphone does that 'for' you. Taking that away, you shouldn't get an error. If in front of your 'if' there is no whitespace, then in front of your 'else' there also must not be whitespace. The statements that belong to the blocks need to be indented though.
16th Apr 2019, 2:16 PM
HonFu
HonFu - avatar
+ 1
Please show us your code, then we can tell you what exactly is wrong.
16th Apr 2019, 1:41 PM
HonFu
HonFu - avatar
+ 1
Are you using indentation in front of that else? (You shouldn't.)
16th Apr 2019, 2:11 PM
HonFu
HonFu - avatar
+ 1
So you made an empty line? Okay, you must not do that. Python doesn't know then, that the else belongs to the if. Immediately after the if block ends, there needs to be the else block. (Some things in interactive mode are different: You 'finalize' a block by hitting enter twice.)
16th Apr 2019, 2:25 PM
HonFu
HonFu - avatar
+ 1
Thank you sooooo much
16th Apr 2019, 2:30 PM
Aman Agrawal
Aman Agrawal - avatar
+ 1
Finally, :) sorry I killed your so much time
16th Apr 2019, 2:30 PM
Aman Agrawal
Aman Agrawal - avatar
0
X=5 If x == 4: Print ("hello") else: (now when I hit enter to write next line it says Syntax error)
16th Apr 2019, 1:43 PM
Aman Agrawal
Aman Agrawal - avatar
0
Check this please. num = 7 If num == 5: print("hello") else: (now when I hit enter I got SyntaxError: invalid Syntax)
16th Apr 2019, 2:02 PM
Aman Agrawal
Aman Agrawal - avatar
0
Are you trying this in interactive mode in IDLE on your computer?
16th Apr 2019, 2:07 PM
HonFu
HonFu - avatar
0
Yes
16th Apr 2019, 2:08 PM
Aman Agrawal
Aman Agrawal - avatar
0
It's showing error both ways
16th Apr 2019, 2:13 PM
Aman Agrawal
Aman Agrawal - avatar
0
But when I wrote the same code on cellphone it works fine.
16th Apr 2019, 2:13 PM
Aman Agrawal
Aman Agrawal - avatar
0
Yeah first letter was capitalised by smartphone. Yeah I haven't used any whitespaces as you said. Can you please write a code with else so that I can post in my pc to check if I'm missing something .
16th Apr 2019, 2:18 PM
Aman Agrawal
Aman Agrawal - avatar
0
Did you manually write your code there or did you copypaste it from somewhere?
16th Apr 2019, 2:18 PM
HonFu
HonFu - avatar
0
if 1: 1 else: 2
16th Apr 2019, 2:18 PM
HonFu
HonFu - avatar
0
No I did manually
16th Apr 2019, 2:18 PM
Aman Agrawal
Aman Agrawal - avatar
0
By writing your code I too get error message after else
16th Apr 2019, 2:20 PM
Aman Agrawal
Aman Agrawal - avatar
0
One thing I forgot to mention I typed enter twice before writing else
16th Apr 2019, 2:20 PM
Aman Agrawal
Aman Agrawal - avatar
0
Hehe, good that we found it!
16th Apr 2019, 2:43 PM
HonFu
HonFu - avatar