Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Python Basics!
      
    
You have completed Python Basics!
Preview
    
      
  Repeating things in code is a frequent need. "while" repeats code until an expression is no longer True. Let's explore how to use it.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      In programming, a while loop is
code that continuously repeats
                      0:01
                    
                    
                      until a certain condition
is no longer met.
                      0:04
                    
                    
                      If I think about my day today,
I've already used a few of these loops.
                      0:07
                    
                    
                      In the shower this morning,
while there was soap on my body, I rinsed.
                      0:12
                    
                    
                      Afterwards, while I was wet,
I dried my body.
                      0:16
                    
                    
                      There's two loops and
I was just waking up.
                      0:19
                    
                    
                      Then while my coffee cup was empty,
I filled it, once my cup was full,
                      0:22
                    
                    
                      I stopped pouring, while there was cereal
left in my bowl, I took some bites.
                      0:27
                    
                    
                      Loops occur all the time in all
applications that we build,
                      0:32
                    
                    
                      while data is loading,
show that waiting spinner.
                      0:35
                    
                    
                      While the deal that will expire eventually
hasn't yet, update the countdown clock.
                      0:38
                    
                    
                      While the user hasn't entered the correct
password, prompt for a retry.
                      0:43
                    
                    
                      That's a great idea.
                      0:48
                    
                    
                      Let's write a simple little
password checker loop.
                      0:49
                    
                    
                      So this is not going to be very robust and
you should never actually use this code
                      0:53
                    
                    
                      in a final application but it's a pretty
good example of how while loops work.
                      0:59
                    
                    
                      So I'm just giving you this warning now.
                      1:04
                    
                    
                      This is like one of those do not
try this at home warnings, okay?
                      1:06
                    
                    
                      Right, so let's do this,
let's create a new file, we'll say File,
                      1:10
                    
                    
                      New File, and
we will call this password_checker .py.
                      1:17
                    
                    
                      So what we'll do right from the start
is we'll prompt for a password.
                      1:25
                    
                    
                      It'll say password = input.
                      1:29
                    
                    
                      Please enter a super secret password.
                      1:33
                    
                    
                      This is not a, it's the.
                      1:41
                    
                    
                      Please enter the super secret password.
                      1:44
                    
                    
                      Gave some space there,
now already that's a bad idea.
                      1:49
                    
                    
                      So here this password is
gonna be right on the screen.
                      1:53
                    
                    
                      Right as you type it out.
                      1:56
                    
                    
                      Someone could look over their shoulder and
capture it.
                      1:56
                    
                    
                      Do not try this at home.
                      1:58
                    
                    
                      Okay.
                      2:01
                    
                    
                      So if the user doesn't get the password
right, we should let them try again.
                      2:01
                    
                    
                      Password typos are super common.
                      2:06
                    
                    
                      So what we'll do is we'll use a new
keyword that kicks off our loop.
                      2:09
                    
                    
                      And that word is while and that's
followed by an expression that will be
                      2:13
                    
                    
                      checked each iteration through the loop.
                      2:18
                    
                    
                      This is very much like an if statement,
so if the expression is true,
                      2:21
                    
                    
                      continue the loop.
                      2:26
                    
                    
                      So that expression is if password is
not equal to whatever the password is.
                      2:27
                    
                    
                      So we'll say opensesame,
that's my password by the way.
                      2:34
                    
                    
                      So we've got a colon, so we're into
a block of code that will repeat.
                      2:40
                    
                    
                      And that's what we'll do
is we'll just ask again.
                      2:44
                    
                    
                      And we'll say password = input
                      2:45
                    
                    
                      ("Invalid password, try again: ").
                      2:50
                    
                    
                      And what happens is after the last
line of the while block is finished,
                      2:55
                    
                    
                      there's only one right now, so
after this line is finished,
                      2:59
                    
                    
                      execution returns to
the while expression again.
                      3:02
                    
                    
                      And if this is true,
it will run the block again.
                      3:05
                    
                    
                      So let's go ahead and write a message so
that we can see if we were successful,
                      3:10
                    
                    
                      let's see, let them into this little
secret world we have over here.
                      3:14
                    
                    
                      So welcome to secret town.
                      3:18
                    
                    
                      Okay, let's give this a run, so
                      3:21
                    
                    
                      we'll say python password_checker.
                      3:25
                    
                    
                      Enter the super secret password.
                      3:31
                    
                    
                      So I'm gonna enter in food,
invalid password, please try again.
                      3:36
                    
                    
                      So because password, which was food,
                      3:42
                    
                    
                      did not equal open sesame,
this line of code and so
                      3:45
                    
                    
                      we're here now, invalid password,
please try again.
                      3:50
                    
                    
                      Again, let's print it out to the screen.
                      3:55
                    
                    
                      Let's do one more,
the password to my luggage.
                      3:57
                    
                    
                      And you'll see that, again,
12345 is not equal to opensesame.
                      3:59
                    
                    
                      And it will continue until
this condition is false.
                      4:04
                    
                    
                      So let's make that false,
opensesame is actually equal
                      4:08
                    
                    
                      to opensesame, welcome to secret town.
                      4:13
                    
                    
                      Again, this is totally not secure at all,
anyone could actually just read this file
                      4:18
                    
                    
                      here and see our password in plain
text right there, not a good idea.
                      4:22
                    
                    
                      Another thing to take note of is this,
                      4:26
                    
                    
                      we'll never see that retry
message if this is ever false,
                      4:29
                    
                    
                      the loop would never run because
the condition is false from the get go.
                      4:33
                    
                    
                      You know what?
                      4:38
                    
                    
                      Let's add an additional check, we don't
want hackers to keep on trying and
                      4:39
                    
                    
                      using brute force to
figure out our password.
                      4:43
                    
                    
                      Let's only allow them three
attempts before we break out of it.
                      4:45
                    
                    
                      So we'll keep track of our attempt count.
                      4:49
                    
                    
                      So this one here,
we'll say attempt_count = 1.
                      4:53
                    
                    
                      And then we'll increment by one
after each password attempt.
                      4:59
                    
                    
                      So we'll say, here,
we'll say attempt_count.
                      5:04
                    
                    
                      And much like the in-place
addition that we did on strings,
                      5:09
                    
                    
                      you can do that on numbers too.
                      5:11
                    
                    
                      So attempt_count += 1.
                      5:13
                    
                    
                      So that's attempt_count =
attempt_count +1, more or less, right?
                      5:15
                    
                    
                      Just some nice short hand.
                      5:19
                    
                    
                      So at the start of this loop, we can check
and see if the count is more than three,
                      5:21
                    
                    
                      all right?
                      5:26
                    
                    
                      So we can say if attempt_count > 3.
                      5:27
                    
                    
                      What should we do?
                      5:33
                    
                    
                      So you can actually stop a program running
but first we need to import a module and
                      5:34
                    
                    
                      that module name is sys, which is not for
sister, it's short for system.
                      5:40
                    
                    
                      Import sys.
                      5:46
                    
                    
                      And the name of the function
that we're gonna call is exit.
                      5:48
                    
                    
                      So we'll do sys.exit.
                      5:51
                    
                    
                      Now the way that sys.exit works,
                      5:56
                    
                    
                      if you pass any value to sys.exit,
it's considered an error.
                      5:58
                    
                    
                      So whoever ran the program will get back
the fact that an error happened in their
                      6:04
                    
                    
                      code, which is kind of what we
want to have happened here.
                      6:07
                    
                    
                      So let's give it a message here.
                      6:10
                    
                    
                      We'll say too many invalid
password attempts.
                      6:12
                    
                    
                      So let's clear this, see what happens.
                      6:18
                    
                    
                      So we will run python password_checker.
                      6:23
                    
                    
                      Okay, so let's try spam.
                      6:29
                    
                    
                      Well, that didn't work.
                      6:31
                    
                    
                      Let's try lumberjack.
                      6:32
                    
                    
                      No?
                      6:33
                    
                    
                      Cheeseshop?
                      6:35
                    
                    
                      How about hovercraft?
                      6:37
                    
                    
                      So this should be our last attempt, right?
                      6:40
                    
                    
                      Too many invalid password attempts.
                      6:43
                    
                    
                      And see, it printed it out there,
too, really nice.
                      6:44
                    
                    
                      Were those password choices confusing?
                      6:47
                    
                    
                      One thing that I always like to make sure
that people know when they're just getting
                      6:50
                    
                    
                      started learning Python is that
the language is not based on the snake.
                      6:53
                    
                    
                      It is quite possibly,
                      6:58
                    
                    
                      surprisingly, named after the British
comedy group Monty Python.
                      7:00
                    
                    
                      Now, I like to mention this because you're
gonna see Python code in documentation and
                      7:05
                    
                    
                      blog posts that use some
very strange examples.
                      7:09
                    
                    
                      You'll see lots of spam and
eggs, and lumberjacks, and
                      7:13
                    
                    
                      hovercrafts full of eels
which is super weird.
                      7:15
                    
                    
                      Especially if you don't know that all of
these are based on Monty Python skits.
                      7:19
                    
                    
                      So actually,
I do recommend watching some Monty Python.
                      7:23
                    
                    
                      Now not only because they are some of
the most amazing absurdist comedy sketches
                      7:27
                    
                    
                      ever created but also because it will
help with some of the more inside jokes.
                      7:31
                    
                    
                      It'll help make sense.
                      7:35
                    
                    
                      I always feel for those of you who
might be perplexed by the references.
                      7:37
                    
                    
                      But also I can not even imagine
what that must be like for
                      7:41
                    
                    
                      learners who have English as a second or
third language.
                      7:44
                    
                    
                      A parrot is a speaking bird, no?
                      7:48
                    
                    
                      Why is the code talking about
a parrot being electrocuted?
                      7:50
                    
                    
                      Is it because it speaks?
                      7:54
                    
                    
                      No, I'm sorry, it's because of a joke
that was made in the late 1970s.
                      7:56
                    
                    
                      How did you miss that one?
                      8:01
                    
                    
                      Check the teacher's notes for more.
                      8:02
                    
                    
                      One more thing that I'd like
to tackle stylistically here
                      8:04
                    
                    
                      is that I don't like how our password
is in the middle of this code,
                      8:07
                    
                    
                      it's kind of down here in this code,
right?
                      8:10
                    
                    
                      It's a value that we
might want to change but
                      8:13
                    
                    
                      it will remain constant during
the running of this program.
                      8:16
                    
                    
                      So one thing that we can do is to create
a variable near the top of the file and
                      8:19
                    
                    
                      I'm gonna call it, underneath our import
here, I'm gonna call it MASTER_PASSWORD.
                      8:24
                    
                    
                      Now note that I used all capital letters.
                      8:30
                    
                    
                      This is a naming convention for constants.
                      8:33
                    
                    
                      So we'll say MASTER_PASSWORD and I'm gonna
get rid of this here, put that here.
                      8:35
                    
                    
                      And then I'm gonna use
MASTER_PASSWORD here.
                      8:40
                    
                    
                      This MASTER_PASSWORD is not something
that I ever plan on changing while
                      8:46
                    
                    
                      the program is running.
                      8:51
                    
                    
                      It's a constant value.
                      8:52
                    
                    
                      And I'm conveying that to readers of this
code by using all capital letters and
                      8:54
                    
                    
                      placing it at the top of the file so
it's the first thing that they see.
                      8:58
                    
                    
                      And then I'm gonna use
that in our while loop.
                      9:02
                    
                    
                      Now if we ever wanted
to change the password,
                      9:05
                    
                    
                      you just need to tweak
the constant variable.
                      9:07
                    
                    
                      Check the teacher's notes for
more on constants.
                      9:10
                    
                    
                      While loops are great for
                      9:12
                    
                    
                      when you want code to run until
a condition is no longer true.
                      9:13
                    
                    
                      Now, in this case,
you aren't sure when the loop will end.
                      9:18
                    
                    
                      There's another type of
loop that is great for
                      9:22
                    
                    
                      when you have a certain amount
of items to loop through.
                      9:25
                    
                    
                      It's called for and I'll show you what
it's good for right after this break.
                      9:27
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up