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