Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Using Break and Continue

Give Feedback

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")

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

Ricky White RP Team on March 23, 2020

Hi phalimem. Be sure to watch the other basic courses to get a grasp on all the different conditional statements available in Python. That will help you find the answer to your question.

realpython.com/python-conditional-statements/

Become a Member to join the conversation.