While Loops
Loops are very important in computer programming. Loops are structures of repetition. We use loops to repeat the same lines of code multiple times. We can also use loops to iterate over lists, tuples, dictionaries, and even strings, as I will show you later. Let's start with the "while loop." First, let's start with a variable called while x and assign a value of zero. Then let's create a list of people that is going to start as an empty list, and then we start a loop writing a condition. Let's visualize it below.
As you can see there's an indentation just like the "if structure", so everything that is aligned under the indentation is going to be inside the while loop so if this test "while x < 5:" returns true it is going to go inside this loop. Once inside let's create a new variable called a person and append this person to the list called people
We are done here, but if we leave it like this, this loop is going to run forever. Because when we finish here, it is going to keep running again and again until x < 5 doesn't run anymore. However, this is not going to happen because we are not changing the value of x.
So what we need to do is in every iteration of this loop, we have to increment the value of x.
We can do it by typing x = x + 1. This is one way of incrementing the value of a variable. You can also increase the value of x by using the increment operator, x += 1. This mean x equals x plus one. So this is going to run in a loop until the test returns false. When this happens then we are going to continue with our program. As we run the program, you can enter any name of your choice and the list will be updated.
Our program runs the loop five times, and when that test returns fault, it continues with the rest of the code, which was this ['John', 'Mary', 'Peter', 'Paul', 'George'] print statement. We could have done this with fewer lines of code.
Since we are adding people to a list in every iteration of the loop, we can use the length of the list to control the loop. Instead of using x, we can use the length of the list called people, as shown below.
So I erased x = 0 and applied the len(length) function to the list, which contains zero people [] in the first iteration of the loop. However, as we add more people to the list, the number of people in the list is going to be greater than 0. And when the number of people is 5 or greater than 5, we are going to jump outside of the loop, so in this case we don't need to increment the value of any variable; hence, x += 1 must be erased.
And... we just this that. Let's try running it again to see what happens.
And we have the same results. We can also jump out of loops using the break statement. Let's write a program to make a guess game.
In this case, let's start with a variable called number, assigning a value of 5 to it. Secondly, let's create a variable called guess and ask for a user's input. Note that I will compare the user input into an integer so that we can compare with the number 5. We will give the user multiple chances so that he can guess; for that, we can use a loop. In this case, let's start a loop that we will run forever.
Meaning every time that test returns true, the loop is going to be executed. So in this case, the loop is going to be executed forever unless we stop the execution with a break statement, which is what we are going to do!
So this means inside the loop, if guess equals (we use double equal signs to differentiate it from assigning a value or variable in other parts of the code), a number, which is 5, means the user guesses the number correctly. In this case, we are going to use a break statement, which will stop the execution of the loop and jump outside of the loop. If that doesn't happen, then the user has to guess again under the else statement. Okay, let's run it to see if our game is working.
let's try number 2 and see what happens.
What just happened here is that the two that was assigned to the variable guess:
as a result, we entered the loop. Since the guess is not equal to number,
we entered the else statement. And now we have infinite chances until we guess the number correctly. So let's try some random numbers.
This is going to run forever until the user guess the right answer. After the user guess the right answer we should have written something like you guessed the right answer to notify the user (print("You guessed it. I was thinking about", number)).
So let's try again:
And it worked. We can even make our game even cooler by picking a random number. We can use the random module for that. So before starting the program we can import the random module. So now the number is going to be random.randint(0,10). It is going to pick a random integer in the range of 1 to 10.
But we still don't know what the number is, so we have to guess.
Really cool right? That was all for this lesson. In our next lesson we will learn about the "FOR LOOP"
See you next time.
No comments:
Post a Comment