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? """
9/8/2020 1:52:17 PMBojan Jovanovic
9 AnswersNew Answer
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!!!
Yes you give spaces because without spaces (indentation) gives a indentation error.
what happens when else statement gets to the end of the line?
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
You can use elif for more readable code.
Nikolai, didn't elif stop after 1st true?
Nematiillo, question isn't about loops, it's about formatting :D But, I get it.
print ("number isn't " "5, 11 or 7" "6 and 12")
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/Python/2278/