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 trialStephen Hancock
4,888 PointsWhile - 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
Treehouse Moderator 28,720 PointsNice 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
4,888 PointsStephen Hancock
4,888 PointsThanks 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.