Else statements formatting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Else statements formatting

num=7 if num == 5 print ("number is 5") else: if num == 11: print ("number is 11") else: if num == 7: print ("number is 7") else: print ("number isn't 5, 11 or 7") """ My question is about formatting, since Python can run 100s of else statements do I reall have to make this spaces? How should that be formatted if else statement gets to the end of the line? """

8th Sep 2020, 1:52 PM
Bojan Jovanovic
Bojan Jovanovic - avatar
9 Answers
+ 2
num = 1189 # num is equal to 1189 for i in range(1200): #loop counts to 1200 if i == num: # if the variable named i is equal to the variable named num print("number is", i) # print the number break # stop the loop """ you do not have to write 1000 lines of code if you use a loop """ # like!!!
8th Sep 2020, 3:26 PM
Nematillo Ochilov 🇺🇿
Nematillo Ochilov 🇺🇿 - avatar
+ 1
Yes you give spaces because without spaces (indentation) gives a indentation error.
8th Sep 2020, 2:05 PM
Akash Agrawal
Akash Agrawal - avatar
+ 1
what happens when else statement gets to the end of the line?
8th Sep 2020, 2:06 PM
Bojan Jovanovic
Bojan Jovanovic - avatar
+ 1
Use a decent code editor, not a regular text editor. A good code editor is smart enough to help you with code indentation (the padding spaces). "Do I really have to make this spaces?" Python is strict with indentation, bad indentation means code won't run, so yes. "How should that be formatted?" Follow this tutorial code 👇 https://code.sololearn.com/cT5BRIbkia21/?ref=app
8th Sep 2020, 2:10 PM
Ipang
+ 1
You can use elif for more readable code.
8th Sep 2020, 2:26 PM
Nikolai Ivanov
Nikolai Ivanov - avatar
+ 1
Nikolai, didn't elif stop after 1st true?
8th Sep 2020, 4:04 PM
Bojan Jovanovic
Bojan Jovanovic - avatar
+ 1
Nematiillo, question isn't about loops, it's about formatting :D But, I get it.
8th Sep 2020, 4:07 PM
Bojan Jovanovic
Bojan Jovanovic - avatar
+ 1
print ("number isn't " "5, 11 or 7" "6 and 12")
8th Sep 2020, 4:13 PM
Nematillo Ochilov 🇺🇿
Nematillo Ochilov 🇺🇿 - avatar
+ 1
The elif statement is equivalent to an else/if statement. It is used to make the code shorter, more readable, and avoid indentation increase. https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2278/
8th Sep 2020, 4:42 PM
Nikolai Ivanov
Nikolai Ivanov - avatar