Python Loop Control, To Infinity and Beyond!

Python Loop Control, To Infinity and Beyond!

While loops are useful when we want to maintain a state until a certain condition is met or until some external event occurs. But they can also get out of hand. Programming is like a circus: you gotta keep the lions in the ring. Or pythons in the loop. It might seem simple, but Python loop control is very important for creating bug-free interactive programs.

Infinite Loops

With great power comes great responsibility. While while loops are incredibly powerful, there’s a pitfall you need to be aware of: the infinite loop.The loop in the following function will repeat endlessly until you force it to quit or the world ends, whichever comes first:

Try it.

Annoyed yet? Press Ctrl + C to break out of the loop. Much like a road trip, it’s important to take breaks. The break statement in Python is a means of escape from a loop.

Python Loop Control Using input(), strip() and break

I promised interactivity in the previous tutorial and now is your moment of Zen. Create a new file,, and enter the following:

The example above introduces two new built-in functions, input() and strip(). The input() function presents a prompt to the user of a program and patiently waits until anything is entered. We are using the strip() method to clean up any spaces that may be accidentally entered into the console when pounding out ‘YES!’ Try it: add as many spaces as you like before or after ‘yes’. They will be stripped away. Lastly, if the text entered at the prompt matches ‘yes’, ‘Yes’, or ‘YES!’, the break statement is called, and we escape from the incessant while loop.

Fun fun fun! But we’re not there yet. Join us in the next tutorial to build on this and write an interactive game, the classic ‘Rock, Paper, Scissors’.

Python Loop Control, To Infinity and Beyond! was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion
  • So-ban khan

    hey guys: Thanks for this wonderful resource, the hello world program.
    I’m a novice and this is helping me out a lot.
    I had a specific question regarding the code on this page ‘python loop control’.
    Q: When I write the code into the IDL or the shell. The execution leads to the display of the text(are we there yet). And the computer waits for my input. But here is the thing, it doesn’t matter what i write(yes, no, random strings) or don’t write, as long as I press enter, the program is done executing. I took out the whole .strip() part but it still does the same . Any suggestions

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid