The listener loop is one of the most common patterns that you'll encounter when using while loops. What the listener loop is, is essentially a pattern that waits for some input or some value before deciding to terminate the loop. So you can only use while loops with the listener loop. Because by definition, a listener loop doesn't necessarily know how many times it's going to run. Even after your program starts, you don't know how many times you're going to actually have to execute the code in a while loop. So for example, what this piece of code does is that it keeps asking the user for a next number to add up. So it's going to ask the user for input and then it asks the user to enter 0 if there are no more numbers to enter. So again before we run this program, we have no way of knowing how many numbers these are actually going to input and so we can't use the for loop. We instead need to use the while loop, and we need to say while the user has not entered the number 0. So let's look at this code. In this code, again, we're adding up the numbers that the user inputs. We're going to add those numbers into this variable, theSum. theSum is going to start out as 0. And then we're going to use another variable, x, to store what these are put in. Here, we're just going to arbitrarily initialize x to be -1. The reason that we do that is because in our while loop, we say, if x != 0, so if we started x out as 0, then we would skip our while loop. So in our while loop, we assign x to be whatever these are inputted, but we passed it to be an integer. So x is going to be an integer representing whatever these are just entered. And then we add that value to the previous value of theSum and reassign theSum. In other words, we add x to theSum. Now when the user has had enough numbers that they've entered and they enter 0, then we're going to exit this while loop and we're going to print out theSum. So let's run our code. So here we're asked what's the next number to add up. I'm going to say 10, and then the next number to add up I'll say 20. And then I've entered 10 and 20 so that's enough for me, so I'm just going to say 0, to say that there are no more numbers. And when I do that, then I can see that I get the sum of 10 and 20 is 30. I can do this any number of times, so I can do 5, 4, 3, 2, 1, and then 0, and then I'm going to get 15. And again, Python isn't going to know how many numbers we actually add up before entering 0, which is why we need to use a while loop here. So this pattern is called the listener loop pattern. Again, we're kind of listening for a particular input. In this case, we're listening for the input is x equal to 0? And while that input is anything other than the termination value, then we keep running our code in the while loop. Let's look at a slightly more complex example. In this code, what we're going to do, is we're just going to ask these if they like lima beans. And we ask them to enter Y for yes or N for no. But here's the problem, when the user inputs a value for whether they like lima beans or not, then they could input anything. They could put in a number, they could put in any other letter. But what we really want is for them to enter Y or N. So what we're going to do, is we're going to keep asking them for a valid input until they enter either Y or N. And then by the time that they've produced a valid input, if they say Y, then we print out Great! They are very healthy. If not, then we print out, Too bad. So let's run our code first to see what we're actually running. So again, we ask, Do you like lima beans? Y or N, I'm going to put in, something else. Now when I hit OK, then I'm going to get this prompt again, and it's going to say, Do you like lima beans? Y)es or N)no:, so I'm going to enter something else. And I'm going to keep getting this prompt until I enter either Y or N. I'm going to say Y, for yes, and I get, Great! They are very healthy. So a couple of notes here, so first, all of these print statements appeared kind of suddenly because of a kind of quirk in the interpreter that we actually use. These should have been printed out as I was inputting numbers. The important thing to focus on here in the output Is that we've printed out, Great! They are healthy when I finally entered Y for yes. Okay, so the important part of this code is this function get_yes_or_no. What this function does is it's going to keep pestering the user until they enter in either Y for yes or N for no. If they enter in any other invalid input, then it's going to ask them again for input. So in this code, we first create a variable that keeps track of whether their input is valid. We initialize that to be False because the user hasn't actually put in anything. And then we say while the user's input is not valid, then ask the user for an answer. And we assign that to the variable answer. We then convert that to be upper case, so lower case y gets converted to capital Y, lower case n gets converted to capital N. And then we say, if answer is either of the valid possible inputs, either capital Y or capital N, then we assign valid input to be True. And what that means is that the next time we actually go through this while loop, valid input is True. And so not True is going to be False. And False is actually good in this case because that means we are breaking out of the while loop and returning whatever these are answered. If the input is not valid, then we instead print out, please enter Y for yes or N for no. That's what get printed out here. And then we go back up to the top of this loop. And we say while not valid_input, valid_input is going to be False meaning that not valid_input is going to be True. Meaning that we're going to ask the user again to enter in Y or N. So as long as the user enters anything other than Y or N, then this while loop is going to keep asking them to enter in yes or no. Until they actually enter in Y or N, in which case we're going to return what they entered. That's all for now, until next time.