In this lesson you’ll see practical examples of the two loop interruption options you’ve learned about in the last lesson. The used examples are debugged in the video, so that you get a sense of what’s happening behind the scenes. The examples covered in this lesson are displayed below.
n = 5
while n > 0:
n = n - 1
if n == 2:
break
print(n)
print("Loop is finished")
n = 5
while n > 0:
n = n - 1
if n == 2:
continue
print(n)
print("Loop is finished")
If you’d like to explore another use case for the break
statement, then check out How Can You Emulate Do-While Loops in Python? from Real Python.
phalimem on March 22, 2020
Hi
How do I create a program where I ask a user to enter a number if the number is not equal 10 then the loop must stop when they enter 10 and add all the numbers the user entered before they enter 10