 A for loop is great when you know in advance the number of times you want to go through the loop. In the cases where you don't know in advance, you need the while loop. Let's look at the while loop using something called the hailstone sequence. Here's how it works. Start with a number, in this case 17. If the number is odd, you multiply by 3 and add 1, giving you 52. If the number is even, you divide by 2. 52 divided by 2 gives 26. 26 is even, divide by 2, giving 13, which is odd, meaning you multiply by 3 and add 1. As you see, the numbers move up and down, much as a hailstone moves up and down in the clouds before finally coming to Earth. In this case, coming to Earth means ending up at the number 1. And it takes 12 steps to get there. The question is, how many steps does it take for a number to converge to 1? And there's seemingly no pattern to that. Some numbers have a short sequence, others have a very long one. For example, if you start with 52, it takes 11 steps to get to 1. If you start at 51, it takes 24 steps. Here's the logic for a program that implements the hailstone sequence. We'll get a number and set our count of number of steps to 0. If the number is not equal to 1, then we're not done yet. And we have to ask if the number is even, in which case we divide by 2, or not even, in which case we multiply by 3 and add 1. That's one more step added to the count, and then we come back to ask, is our new number not equal to 1? If it's not equal to 1, we have to go through the whole process again. Eventually we'll get down to a 1, so number not equal to 1 will come back as false, and we'll print the count, the number of steps that we took. This bold red line here is our while loop. It comes back after we add the count. We come back and test again. Now let's look at the Python code to implement this. At the top of the program, I've taken that flowchart and expanded it into text. On line 10 I ask for the number, and on line 11 I set the count to 0. Line 12 is where the magic happens. As long as the number is not equal to 1, I have to do the body of the loop, which is on lines 13 through 17. I know that's the body of the loop because it's indented one level from the while. For odd numbers, I multiply by 3 and add 1. Otherwise I divide by 2 and add 1 to the number of steps. When I finally reach 1, 1 not equal to 1 is false. I drop out to line 19 and print the number of steps with the count that I've accumulated. Let's view the shell and run the program. This time with 17, which takes 12 steps. If I start with 51, it takes 24 steps. If you want to see the process in action, you can print the number here. And then you will see inside the loop every calculation that has been done. If I type 17, you'll see the sequence. 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1. Which took 12 steps. One other thing that I have to point out here. What happens if I run the program and start with 1? The while loop exits immediately and I have the number of steps as 0. This is an important concept in a while loop. If the condition comes false immediately, the body of the loop will never be done. I entered a 1 for the number. My count was 0. 1 not equal to 1 came out false immediately. So I dropped down to line 20 and printed the number of steps as 0. Let's look at another use of while. Getting input until the user tells us they're finished. Here's a program that asks users to enter prices of items and gives them a total when they've finished. But again, here we don't know how many items they have. They may have 1 item, 10 items, or 50 items. We're going to set up a sentinel value. A special input value that indicates that there are no more data items coming in. In this case, we'll use negative 1 as the price because nothing can cost negative $1. That will be our sentinel value. We start off by setting the running total to 0. And we set up a Boolean variable called more input. Initially true, meaning I'm waiting for more input. I'm going to run the program so I can do the narrative along with the code. I'm waiting for more input. More input is true. That's why I'm asking for the price or negative 1 to finish. I'm going to enter $8. $8 is not equal to negative 1, so it'll add $8 to our running total. We return to line 6. More input is still true. We're still waiting for more input, and that's why I got the prompt again from line 7. This time I'll add $4.50. $4.50 is not equal to negative 1, so that will get added to the running total. I come back up here. More input is still true, and I again prompt for a price. This time I'm going to enter my negative 1. Since negative 1 is equal to negative 1, I'll take the else branch of this selection and set more input to false. Once that happens, I come back to line 6 and ask, is there more input? This time I get false, and I'll drop down to line 12 and print the total of $12.50. Finally, you can use while loops to repeatedly request input until you get a valid value. For example, your program might ask users a yes or no question. You want to make sure they either type the letter Y or the letter N. If they type the letter X or something else, you want to prompt them again and say, no, really, give me a Y or N. That's what this function, yes or no, does. You give it a message, the prompt, to be displayed. It starts by setting the valid input to false. Before you ask for input, you don't have valid input yet. As long as you don't have valid input, the program will put up the message and get your input and convert it to uppercase. If you gave me a Y or an N, then that's valid input and I'll set valid input to true. Otherwise, I'll tell you to enter Y for yes or N for no. The loop will come back and if you did give me a Y or N, valid input is now going to be true, not true as false, and I'll drop out and return the answer. If you didn't give me valid input, I'll go through lines 5 through 10 again. This is the, we're going to keep doing this until you get it right loop. Here's the main program. I'll call get yes or no with the prompt, do you like carrots, Y slash N? I put this Y slash N here so that the user has an idea of what to expect to put in for valid input. Get yes or no will return either the letter Y or the letter N into the variable carrots. If carrots equals Y, I'll print one response, otherwise I'll print a different response. Let's run that script and view the shell. If I type an X, please enter Y for yes or N for no. This is the loop in action. It's going to keep asking me again and again and again until I finally type a Y or an N. This time I'll go with Y and the program ends telling me I like them too. Let's run it again, check with invalid input and then valid input and the program works. The key point of this video is that the while loop lets you repeat some actions as long as some condition is true. You don't need to know the exact number of iterations in advance.