We continue loop today and add the concepts of ‘break’ and ‘continue’

Break is used to exit a loop entirely.
Continue is used to exit only the current iteration.

  1. Type and run

count= 0

while True:
if count==5:
break
print(‘Kurosaki-kun’)
count = count+1
print(“Time for the next episode”)
print(count)

The last two print commands are outside the loop. The first print command and the counter update is inside the loop . Take note of the indentation. The if condition is of course inside the loop.


  1. Type and run

bandListA = [‘Artcell’,’Aurthohin’,’AC/DC’,’Arbovirus’]

for band in bandListA:
if band==’AC/DC’:
continue
print(‘%s is a Bangladeshi band’ %band)

  1. Type and run

Type and run

proposedNames = [‘Iron Man’, ‘Black Widow’, ‘Captain America’, ‘Quick Silver’, ‘Firestar’, ‘Thor’, ‘Invisible Woman’, ‘Scarlet Witch’, ‘Black Panther’, ‘Captain Marvel’,’Falcon’,’She-Hulk’]
confirmedList =[]

for name in proposedNames:
if name==’Quick Silver’ or name==’Scarlet Witch’:
continue
elif name==’Captain Marvel’:

confirmedList.append(name)
print(‘We got Captain Marvel on the team! We need no more!!’)
break
else:
confirmedList.append(name)

print(“The confirmed team for the mission is as follows:”)
print(confirmedList)

  1. You could have done the above with two if conditions instead of if-elif-else. Also, you could have defined a separate list of names as ‘RejectList’ and use ‘continue’ to ignore them. Or you could just do this:

confirmedList =[] # resetting confirmedList as an emptyLIst. ProposedNames is as before so not defining again

for name in proposedNames:
if name in [‘Quick Silver’,’Scarlet Witch’]:
continue
if name==’Captain Marvel’:
confirmedList.append(name)
print(‘We got Captain Marvel on the team! We need no more!!’)
break
confirmedList.append(name)

print(“The confirmed team for the mission is as follows:”)
print(confirmedList)

What is the difference from the previous code in #3?

  1. When is an infinite loop setup intentional? Mostly in cases where we have a safety measure such as ‘break’ to come of the loop when a certain condition is met (well, in that case, we can’t really call our setup an infinite loop, right? Let’s call it a fake infinite loop 😛 )

Type and run

from random import randint #we are importing a function ‘randint’
#from a special library ‘random’
#that will help us generate random numbers

while True:
someNumber = randint(1,6) #generates a number between 1 to 6 inclusive
print(someNumber)
if someNumber==6:
print(‘Chokka!!!!’)
break

Try running the cell where you have written the above code several times! You will get different outputs each time 😀

  1. We now make the code above a little more upgraded to depict real-time ludu playing feelings!

Type and run. Well, run several times 😀
(You have already imported randint – so not importing it again).

chaal=0
while True:
someNumber = randint(1,6) #generates a number between 1 to 20 inclusive
print(someNumber)
chaal=chaal+1
if someNumber==6:
print(‘Chokka!!!!’)
break
if chaal%4==0:
print(‘Uff, why no chokka yet! :/ ‘)

What does the last if condition mean?