0
Help needed coders...here its saying 'break' outside the loop...wat ds that mean
x = 0 while True : print(x) x+=2 if x>=14 : break
10 Antworten
+ 5
Y Åđ Û The break statement ends execution of the nearest enclosing loop or conditional statement in which it appears.
so it basically tells the computer this is the end of my loop or condition
break;
+ 3
thanks Pulkit Kamboj ..got an idea
+ 2
the print statement position doesn't matter at all you shift the break statement in side the loop like this
https://code.sololearn.com/cUS9Mm595kaE/?ref=app
+ 2
thanks Pulkit Kamboj ..but bro...can i kwn y...we shld move break frm the previous postion..jst below the if statement itself is the break statement...and if the if statememt was true..then it shld break na...y did we add more spaces??
+ 2
well it's really tough to explain in some lines but I will say that in Python there are no brackets so indentation(spaces) has a very I important role so in my case I had put the if statement inside the loop(by giving spaces ) but you have not given any spaces inside the if so if is outside the while loop, so in your case the if will only be executed when the while loop will stop and the loop is an infinite loop so it never stops but in my case the if statement is always executed as it's inside while loop.
+ 1
well if you use break outside a loop in python it will surely raise an error because it is used to terminate a loop and has no purpose outside a loop.
+ 1
In the code you have written as the if statement is outside the loop so the break is also outside the loop.In order to correct it you have to put the if inside the loop and that's exactly what I have done.You are making mistake in the indentation.
Hope you have got it.
0
even wen i gave a print statement after break....it still shows...break outside loop :( Pulkit Kamboj
0
x = 0
while True :
print(x)
x+=2
if x>=14 :
print("breaking")
break
print("k")
0
still showing the same Pulkit Kamboj