Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping While Loops

Stephen Hancock
Stephen Hancock
4,888 Points

While - not sure why we won't know when the loop will end.

So at the end of the video it says we don't know when the loop will end when using 'while'.

Now I think I understand if we were just using 'while' and maybe that was what the video was referring to, but we have defined an end point to the loop by adding the sys.ex. when the user has too many invlalid password attempts.

I either don't fully understand how the loop is working or I am misunderstanding the comment.

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 26,984 Points

Nice catch, you are a very observant viewer! The "while" loop keeps running until a condition is met. But, the code slightly contradicts what he is saying since it would EXIT to the system after three incorrect attempts. The EXIT (sys.exit() function) is part of the loop but it "short-circuits" the while loop-- like an escape hatch!

At around 9:30 in the video Craig is attempting to convey that this particular while loop will continue looping until the user correctly matches the MASTER_PASSWORD. He is saying that it might run only one time, but could also run two times, or three times-- it all depends on what the user types. In fact, the user may never get the correct password in three attempts and it will just EXIT.

He is setting up the next video in the sequence where you will use the "for" loop which typically is looping over a finite number of elements.

Stephen Hancock
Stephen Hancock
4,888 Points

Thanks for clarifying this Jeff, it is really helpful.

I thought this might be the case but I didn't want to assume, and carry on with potentially an incorrect understanding.

I like the anology of the (sys.exit() fucntion) 'short circuting' the while loop. It is a helpful anology to 'visualise' how this works.