Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Shopping List App Shopping List Introduction

Joseph Ferrero
Joseph Ferrero
1,308 Points

Why does the "break" stop the program?

In this program I don't understand why the program is actually stopped when you enter done. I understand the the loop will stop, but why does the program itself exit out? Is it because the statement is no longer true because the loop stopped?

2 Answers

28,558 Points

The program doesn't stop as soon as you type done, it exits the while loop and then moves on to the code that prints out the items you entered via the for loop. Once it has done that is has gone though all of the code that is actually written which means there is nothing else for Python to do but exit.

Using the "break" keyword is the same as changing the condition of the while loop to false and skipping the code after the "break" command. It then exists out of the while loop, and if there's any code below the while loop and not inside, it will be run. If there is no code below the while loop, there is nothing for Python to execute so it quits.