The break statement is used for iterrupting flow of a loop, i.e. for, while. These loops are mostly used for iterating the list of elements, array, lists, tuples. The reason to this issue is simple and it happens quite often.
Syntax:
loop start:
if condition:
break
So if the condition goes out of this iteration it fails, like the code below:
for i in range 9:
print(“I am {}”.format(i))
break
Here you need to understand that the break is used to get out of that loop but you are trying to break that iteration out of that iteration, so you need to put that inside the loop and by inside I mean where the iteration takes place like this
for i in range 7:
break
But this is not the way you use it there should be a condition mostly to break a loop in between e,g I have a list [‘apple’,’orange’,’grapes’,’pineapple’,’berry’] and I want to know the index number of orange so I just need iterate this list upto that point and there is no point to iterate it after that so I’ll do is
fruits = [‘apple’,’orange’,’grapes’,’pineapple’,’berry’]
for fruitid in range len(fruits):
if fruits[fruitid] == ‘orange’:
break