Hello everybody and welcome back to this tutorial. And right now we're going to
take a look at a different approach to the while true loops, or basically the
infinite loops. We're going to learn how we can deal with them, how we can
actually close them even though they are infinite, and break out of them with a
simple command. So what we're going to do is we will code the same program from
the previous video. That's why I was so confused, because I didn't want to show
you how to properly code it until we actually get to the break statement
right here. And right now we are going to code a password checker with the
actual while true loop. What this will do is it will actually prompt the user to
enter the password for as long as they don't specify the correct password in
the program. So, first of all, let us write the simple program. We want to open a new
file, so we want to open this right here. Let us save it right away, so save as to
the desktop, we can call it 4thprogram.py. OK, we saved it, and right
now let us actually see how we can do this. Now this will be a little bit
harder than in the previous videos, or the previous programs that we did, especially
for the beginners, because we're going to do something which we haven't done
before, which is going to be embedding an if-else statement into a while true loop.
So you will see, trust me it is not really that hard, it is rather easy, just
try to make sure that you understand it the best you can. So, first of all, we will
start off by actually setting the password as usual to be equal to
anything we want. So we can set it to be equal to, right now let's switch it up,
rainbow is the password. Now let's actually add the capital R right here as
well, just for the thrill. And right here we are not going to actually prompt the
user to input the actual password right here, we are going to prompt the user to
input the password in the while true loop itself. So, you know in the previous video
we did is we actually prompted the user to enter
the password outside of the while loop, and right here we want to type while
true, and then we want to prompt the user for the password. So while True:
inputted_password is equal to, let me just check this out, okay, so is
equal to input, and then we want to specify the user to enter the password. Okay,
so we prompt the user in the while true loop to enter the password. Now if you
just ran this program right now, what this will do is it will basically prompt
the user forever to enter the password. But, we don't want that. We want the user
to actually get the access granted string printed out once
they actually get the password correct. So we can do that with the if-else
statement. So if inputted_password, so
inputted_password, is == to the password, so this is something that
we are already familiar with, we did something similar to this so nothing
really to explain right here, if inputted_password == password we
want to break out of the loop. So, basically, what we are doing right here
is we are breaking out of the loop. In any other case, we are not going to break
out of the loop. It will just go back right here and it will check whether
the inputted password is equal to the password. If it is not it will go right
here, and then back to the beginning of the while true loop, prompting us to
enter the password once again. And if we manage to get it we want to print right
here 'Password Correct!', and then in the next line 'Access Granted!!!'
So, let us take a look at this once again, it is a rather short program. We set the
password variable to have the password itself, then we enter a while true loop
which is an infinite loop. Then we prompt the user to enter an actual password, we
compare these two passwords, so we check whether the inputted password from the
user is equal to the password specified in the program itself. If it is, we're
going to perform a function called break. Now, as I said before this, the break
function will simply break out of this loop, but it will not cause the program,
don't confuse it with closing the program, this will only exit
the while true loop. And then it will continue executing to the
next step, which will print password correct and access granted.
The reason why we are actually printing this right here is because this part
right here can only be accessed once the password is correct. If the password is
not correct this will run forever. So let us see how this will work. If I just go
right here, or first of all let me save it, so save, and then I run it, you can see
we get prompted with enter the password. If I just type here password it will ask me
again. 123, password123,
helloworld, nothing really works. Even the lowercase rainbow will not work, as
you can see right here rainbow will not work. But, once we actually specify a
Rainbow, we get password correct, access granted!
So we were successfully able to actually code a password checker with a while
true loop that has an if statement in it. So this would be a more appropriate way
to actually code a password checker than in the previous video. What we are
going to do in the next videos, we are also going to take a look at our
continue function in Python 3, which is also going to help us with the loops
themselves. So, that would be about it for this tutorial. I hope you enjoyed it and I
hope I see you in the next one. Bye!