Why my IDLE doesn't support else, elif ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why my IDLE doesn't support else, elif ?

I try to work with the else and elif but it only support if condition. I am using python shell 3.7.2 in a windows pc and pydroid3 on andriod.

13th Jan 2020, 3:31 PM
Rudra Sri
Rudra Sri - avatar
11 Answers
+ 2
When you press enter on a blank line the code will be ran in the shell. You can with few tricks avoid this. Easy way might be writing: if condition: #nothing And write code before nothing.
13th Jan 2020, 3:49 PM
Seb TheS
Seb TheS - avatar
+ 2
Sab Thes Thank You for the trick 😅
13th Jan 2020, 3:55 PM
Rudra Sri
Rudra Sri - avatar
+ 2
Rudra That's actually a syntax error, you should not include elif statement in if statement's code block. This is invalid: if condition: elif condition: pass
13th Jan 2020, 4:04 PM
Seb TheS
Seb TheS - avatar
+ 1
num = 7 if num == 5: print("Number is 5") elif num == 11: print("Number is 11") elif num == 7: print("Number is 7") else: print("Number isn't 5, 11 or 7")
13th Jan 2020, 3:42 PM
Rudra Sri
Rudra Sri - avatar
+ 1
Jan Markus I don't think reinstalling is the solution for this problem.
13th Jan 2020, 3:51 PM
Seb TheS
Seb TheS - avatar
+ 1
Jan Markus I didn't but i think i have to reinstall it and it may solve the problem
13th Jan 2020, 3:54 PM
Rudra Sri
Rudra Sri - avatar
+ 1
Rudra Was the problem that the code starts to run before you reach to write elif and else statements?
13th Jan 2020, 3:55 PM
Seb TheS
Seb TheS - avatar
+ 1
Sab Thes When i reach elif and else statements it shows if 2 ==1: print('something') elif 2 == 2: SyntaxError: invalid syntax And i can't even write print()
13th Jan 2020, 4:02 PM
Rudra Sri
Rudra Sri - avatar
+ 1
Sab thes Can you show me a code example please Where i can use else or elif statements?
13th Jan 2020, 4:09 PM
Rudra Sri
Rudra Sri - avatar
+ 1
Indentation is necessary in Python, or it won’t compile correctly. Can you delete the whitespace before your elif and try again before pressing Enter after your elif ? Have you tried to write your code in Notepad for example (with indentation) and copy-paste it in your IDLE and run it ?
14th Jan 2020, 10:57 PM
Jeremy Lambert
Jeremy Lambert - avatar
+ 1
Sab Thes When i reach elif and else statements it shows if 2 ==1: print('something') elif 2 == 2: SyntaxError: invalid syntax And i can't even write print() _______________ Its wrong code. You need write this: if 2 == 1: print('something') elif 2 == 2: print('honey, used four spaces and correct indents') Put your IDLE to trashbox. Install "PyCharm Comminity Edition". It's good thing. Freeware.
27th Jan 2020, 1:18 PM
Valerii Mamontov
Valerii Mamontov - avatar