+ 1

how to break out all the loops in python

count=0 for i in (1,2): while count<5: count=count+1 hi="*"*count print(hi) if count==5 : while count>=1: count=count-1 yo="*"*count print(yo) can someone tell me how to break out all the loops

8th May 2021, 11:51 PM
Hosein
Hosein - avatar
5 Réponses
+ 1
I think using function is better.If you take it in a function ,then you can break out by returning.
9th May 2021, 12:15 AM
The future is now thanks to science
The future is now thanks to science - avatar
0
Use boolean variable to note that the loop is done, and check the variable in the outer loops to execute a second, and third break
9th May 2021, 12:07 AM
Michal Doruch
0
as stated by others, breaking a loop early isnt really recommended. with that being said, you can break out of a loop early bu inserting “break”. example: for i in range(10): if i == 5: break this example will break out of the loop when i reaches 5.
9th May 2021, 1:19 AM
you are smart. you are brave.
you are smart. you are brave. - avatar