While Loops

Programmers love to find ways to make their lives easier. They also hate to repeat themselves over and over again. One tool that they have at their disposal to help them to make things easier and avoid duplicating their code is loops. Loops, like the term hints at, allow you to repeat something over and over again. The first type of loop that we are going to look at is called a while loop. A while loop will repeat over and over again as long as a given condition is true.

While loops are very useful and have many applications. One that you will use quite often is to loop within games. In many games you give the player multiple live or attempts at playing the game and when they have no more lives/attempts left you will end the game. But you do not want to have to copy and paste your code five times if they have five lives, so you use a while loop. Remember that while loops run over and over and over again until a specific condition is no longer true. This means that you need to a little careful because it can be easy to get stuck in an infinite loop that never ends. If this happens you’ll need to force quit your program. Let’s look at what a program that asks the user over and over for their full name would look like.

#write your full name here

fullname = "Hank Willis Moncton"

enteredName = input("What is your name?")

while fullname != enteredName:

      enteredName = input("Sorry, please enter your full name:")

print("Excellent! Thanks " + enteredName + ".")

You can see that this this little name program will repeatedly ask the user for their full name (or at least the name that the program thinks is their full name!). For as long as the variable fullname does not equal enteredName the loop will run and ask again for the user’s full name.

Now that you have a basic understanding of while loops let’s look at how it might be used in a game to keep the game running while the player still has lives left. To do this we can turn our name program from above into a little game where the user has 3 chances to get their name correct (pretty easy game!). In our program we are going to need a counter that keeps track of how many attempts the user has made. We will also have to update our while loop to only ask if they have any guesses remaining.

#write your full name here

fullname = "Hank Willis Moncton"

enteredName = input("What is your name?")

guessCount = 1

while guessCount < 3:

      enteredName = input("Sorry, please enter your full name:")

print("Excellent! Thanks " + enteredName + ".")

We have a good start with the code above but there are definitely a few problems. What is wrong with the program when you run it? Does it actually stop asking after 3 attempts? Also, when you enter the correct name does it stop and give you the success message? Nope and nope!

Let’s start by fixing the problem of the while loop not ending when the user enters the correct name. There are a few ways that you could do this but a nice clean and easy way is to use a logical operator.

and

x > 1 and x < 5 produces True only if x is greater than 1 AND x is less than 5

or

x > 1 or x < 5 produces True only if x is greater than 1 OR x is less than 5

not

not x < y produces True if x is NOT less than y

We can update the while loop to check to see if the number of guesses is still less than 3 OR if the guessed name and the actual name do not match. This will make our game loop stop if either of those conditions is no longer true.

The other issue is that our loop does not stop when we have made 3 guesses. Why is that? You may have noticed that we never actually increase our number of guesses counter. We can fix this by incrementing the guessCount variable by one after the user has entered another name. Python actually has abbreviated assignment operators that allow you to increment variables by specific values.

+=

x += 1 will increase x by 1

y += 3 will increase y by 3

-=

x -= 1 will decrease x by 1

y -= 3 will decrease y by 3

*=

x *= 2 will multiply x by 2

y *= 3 will multiply y by 3

/=

x /= 2 will divide x by 2

y /= 3 will divide y by 3

Now we should be able to increase the guessCount by one within our while loop and this should prevent the loop from running infinitely. Try it out and see if you can get our goofy little name guessing game working.

Things are looking pretty good but when the program ends there is no message telling the user if they were successful or not. Let’s write some code that tells the user “Great job! You know your name!” if they got it correct and “Sorry! Only 3 guesses!” if they are unsuccessful. Think carefully about where you will put this code. As a hint, remember that you want this code to run when the while loop is finished. Also, if you are not sure how to check if they were successful with their name guessing, think back to our work with conditionals.