Hey, in this video, we will look at another wild loop example so that you understand the wild loop
from another perspective.
I'll show you two examples, actually.
The first one is a very small program where we ask the user to enter a password and then we try to check
if the password is correct.
If it is correct, we tell the user that the password was correct.
If it's not, we continue to ask the user to enter a password over and over again.
This is how I would do it with a wild loop.
So think about the user again.
What do you want to show them first?
Well, ask them to enter a password, so get the password from the user using an input function.
Enter password.
So that's the message we show to the user.
The user will enter a password and the password that the user enters will be stored in this password
variable.
Then we check wild passwords.
So while that variable value is.
Not equal to.
Pass.
One, two, three.
So let's suppose that the correct password is pass.
One, two, three.
And that's how we start the loop.
So what is this expression here?
And what is this?
Let me show that to you in the Python console.
You know, if you do pass 1 to 3 equal to pass 1 to 3, what you get is true If I execute this because
this is an equal operator, so you get through the opposite of an equal operator is the not equal operator,
which comes with this exclamation mark instead of two equal operators.
So if I press that, I will get false.
So what I'm saying here is that while the password is not this, then we keep asking the user for a
password.
So we execute that again over and over again.
Otherwise, I will only indent now and go outside of the one loop.
Otherwise we say password was correct.
So let me execute this.
Now enter password.
Let's say pass one.
Oh no.
Pass two.
No pass.
One, two, three.
Yes, password was correct.
So to wrap it up, this is how this code works.
This line here is executed only once.
Everything that is outside a loop is executed only once in Python.
But this expression here is inside the loop, so it will be executed as far as this condition stays
true.
And that condition is dependent on what the user enters here.
So as soon as the password that the user enters is different from pass 1 to 3, we will get this executed
over and over again.
Otherwise, the loop ends and we show the user this message.
That's how this code works.
Another example now.
I'll rename this to.
Bonus to that one and create a new file.
Bonus to the two.
This time I want to use the World Loop to produce a series of numbers as output.
So I want to produce numbers from one, two, three, four, five, six.
So one, two, three, four, five, six will be printed out in the command line.
How can we generate these numbers?
Well, let me show you how using the one loop while.
But we start with a value.
Let's say x is one.
X is a variable.
One is a number.
And we say while x is.
Less or equal.
Then six.
We do something.
And that here is also a comparison.
Operator Just like we had that not equal.
Operator We also had this equal operator.
We also have the comparison Operators such as this one, for example, is one less than three we get
through is one greater than three, we get false.
Similarly, we have.
Less or equal.
So we get true because one is less than three.
If you say it's one less or equal to three, sorry, equal to one, you get true because one is equal
to one.
And so on.
So that's what we're checking here.
If X is one, two, three, four, five or six.
This will be evaluated to try to.
So then I'll print out.
X.
But if I run the script now, what's going to happen is we're going to get one, one, one, one printed
out all the time.
So I have to stop the program now.
Otherwise my processor will get quite busy.
So we get one printed out because X here will always be less than six since its value is one.
So there's no dynamics here.
Nothing is changing x therefore we will get this line printed out all the time.
But if we change x and say x is x plus one, then what we're doing is.
We declare X here equal to one.
We check if X is less or equal to six, which is true.
So we get printed out one.
So in the first iteration we will get one printed out, but then here X will be overwritten with this
value.
So currently x is one, therefore one plus one gives two.
So the new value for x will be two and then the loop goes again.
And here.
X now is two, which is still less than six.
So the loop will print out this again, it will update x, it will say x is equal to two plus one and
that will make x three.
The loop goes again, print three and so on.
So let's run this.
And that's the output.
One, two, three, four, five, six.
So that output was also my intention, and I hope this gives you some more understanding about the world
loop.
Thanks for following.