break
is useful if you want to end the loop part-way through.
while True:
print('Please type your name.')
name = input()
if name == 'Mahmoud':
break
print('Please try again')
print('Thank you!')
If you do this with while name != 'Mahmoud':
, it will print Please try again
at the end, even though you typed Mahmoud
.
while True:
means to loop forever (or until something inside the loop breaks you out), since the looping condition can never become false.
0
solved Is there a use of break statement in python?