Now that we've learned all about loops in Python,
we're finally ready to tackle our final project of the day,
which is the PyPassword generator.
Our program will ask us how many letters would you like in your password?
So I will say I would like a long password, let's say 14.
How many symbols, 3, how many numbers, 4,
and it's going to generate me a password that is almost completely impossible to
crack by any hacker, no matter how talented they are.
So the project of today is to build this password generator.
If you head over to this particular Repl.it,
password-generator-start, then you'll see that here
I've already included for you a list of all the letters in the alphabet from A
to Z, including the uppercase and lowercase letters.
I've got a bunch of numbers and a bunch of symbols that are usually accepted by
websites in the passwords.
And I've also created the inputs that you saw when we did the demo.
So it's going to ask us how many letters, how many symbols, how many numbers,
and then you're going to turn all of that knowledge as well as using these
lists
and what you've learned about loops into the same password generator that you
saw in the final demo.
So have a play around with the final demo yourself,
and then have a think about how you might be able to achieve this goal of
creating a random password for the user based on the number of letters,
symbols, and numbers that they want.
Now there's two levels to this project.
You can either go for the easy version where all you have to do is generate your
password in sequence. So for example, if the user wanted four letters,
two symbols, and two numbers,
then it might just look like this four letters and then two symbols and then two
numbers. So they're in sequence right? First, you've got the letters,
then you've got the number of required symbols,
and then you've got the required numbers. So this is the easy level.
And then there is the hard level. And the hard level requires you to generate the
password in a completely random order.
So instead of having it being, letter symbol number,
it should be maybe letter and then a symbol and then a number,
then a letter, then a symbol and then a number.
But notice how this is completely random.
Every single time you generate it,
the order of where the symbols and where the numbers appear in your password are
completely random. So for example,
if you look at the demo version and I do the same thing,
four letters, two symbols,
two numbers, where the numbers and where the symbols occur are completely
unpredictable. And every single time I generate a new password,
it's going to switch it up again.
So that's the hard level. I recommend trying the easy level first
and then once you've managed to do it, then take yourself up to the hard level
if you think you're up to it. But remember that if your doing the hard level,
you might have to do a little quick Google around in order to figure out how to
do something that you might not have seen before.
But it's all part of the challenge and I hope you'll take it on with vigor.
So pause the video now and try to complete the final project.
Alright,
so let's first tackle the easy level where we're just generating a password
with the required number of letters and then the required number of symbols and
then the required number of numbers.
So let's go ahead and tackle this one first.
Now we know that we've got these lists which we can draw from,
and we know that we can use the random module to get hold of a random item from
the list. So all we have to do is first get four random letters.
So let's say that we had a password
which just started out as an empty string.
Then we can use a for loop to loop through all of the characters that we need to
create for our password. For example,
I might say for char, short for character, in the range of,
um,
from 1 to the number of letters that the user wanted.
So in this case, if the user wanted four letters,
then it will be a range from one to four, but not including four.
So it's actually from one to three,
which has only three characters. In order to modify this,
we have to do what we did always before,
which is just to add one to the end of our range.
So now we're going to create a range based on the number of letters that the
user wanted.
And I'm just going to go along with the assumption that the number of letters
equals four. Let's just pretend that's what the user will enter.
So then this range becomes 1, 2, 4 + 1 which is 5.
So the range is in fact going to be 1 to 4. For every single number in that
range from one to two to three to four,
I'm going to generate them a random letter,
and I'm gonna pick it out of this list of letters. To do that
I can either generate a random number and use it as the index or you might've
seen previously
I showed you the random.choice function. And inside this random.choice,
we can pass a sequence
which in our case is the letters list.
Then it will look through this list of letters and it will give us a random item from
that list.
So now that random letter is going to be something that we're going to need to
add to our password, right?
So we can set this equal to a random character.
And now let's go ahead and just print it out.
Right?
So let's say we want four letters in our password and we can type anything for
the other ones, but notice how when the for loop runs,
it's given us some random characters that it's picked out of our letters list.
So there's an a,
there's a capital P there's a d and there's a capital J. How can we add each of those
letters to our password? Well,
we can use the string concatenation that we learned very early on in Day 1.
We can say password equals the previous password
plus the random character. Of course,
the shortened version of this is just password+= random_char.
And now let's go ahead and instead of printing the random character,
let's print the password at each stage of the loop.
Right?
And again,
I'm going to choose four letters and you can see that the first time the loop
runs I get an M. So password is equal to M. The second time I get a Q
so I add the Q onto the M, I get MQ and so on and so forth until I get these four
characters in the beginning of what seems to be my random password that's going
to be generated.
Right
Now, if you wanted to,
you can actually simplify this down to just one step,
because instead of having this intermediary,
which is the random character variable,
we can simply just replace it with the code, like this.
So in this case, we're getting a random choice from our letters list,
so we get a random letter, and then we add that to our password.
Now that we've done that for the letter,
then it's pretty easy to see how we can continue doing this for the symbol and
the numbers. We just have to keep adding it to the end of our password.
So let me quickly write out the code for this.
Right?
So now when the user requests that they want four letters in their password,
then we generate a range from one to five.
And then for each of those positions,
we generate a random letter in order to fill the password.
And then we add on the required number of symbols and the required number of
numbers. And finally, we print out the final password.
Let's go ahead and run this code. And again,
I'm going to choose four letters, two numbers, and two symbols.
And I end up with my final password,
which fits exactly that criteria and it looks pretty random.
But the only problem is that if a hacker knew that you always have two symbols
at this position, and two number at this position,
then it's very easy to crack that password because in order to get the last
digit, for example, they only have to try nine different variations, 12
3456789.
And this just limits the strength of your password. In order to tackle that you
would have to tackle it at the hard level,
which is how do you get a password that has all of these letters and numbers in
a completely random order. To do that
we mentioned that you have to do a bit of googling because it might require
something that you haven't done before. But in essence,
you've got access to everything you need to perform the logic.
So if you haven't given that a go,
then this might be a good time to pause and have a think about how you might
structure the logic or how you might create a flow chart to express this logic
that you would want. And once you've done that,
and if you want to see the solution and walk through it with me,
then come back here and I'll show you how to do it, but first give it a go.
Right?
All right. In order to tackle the hard level,
what we essentially need to do is instead of adding each of these letters into a
string, we actually want to add it into a list.
So I'm going to copy all of this that's here,
and I'm just gonna comment it out and I'm going to paste it under my hard level and
work on it here. So instead of using a password string,
I'm going to create a list and to better describe it
I'll change my variable name, the password list. So now,
instead of using password to add each of these letters and numbers and symbols,
I'm going to be adding it to my list.
Right,
Right
Now, to add things to a list,
you can use the same syntax that you did with strings,
which is to add the new item into the list and to update the old list.
You might be more familiar though with the syntax
which is append and both of these work.
You can use either of them if you want, but they'll end up doing the same thing,
which is to add each of those things you've generated in the order that you've
generated to your password.
So we end up now with a list with four letters,
two symbols, and two numbers,
all stored as strings because of the quotation marks around them.
So now that we've got a list, well, then we have to think about, well,
how can we shuffle our list,
how can we mess up the order of the items in our list?
And that might take you to Google and you might search for something like how to
change the order of items in a list.
And then we also have to add our keyword, which is Python,
because there's obviously a lot of other programming languages.
And we come up against our first Stack Overflow question,
how can I reorder a list? Well, that sounds kind of like what I want to do,
right? So if we scroll down,
you can see that the way to do it is either using a for loop,
like so, or
to simply use the shuffle function.
This is a much simpler way because it allows us to update the existing list just
by calling the shuffle function on it.
And this one actually requires us to create a new list and we have to create a
bunch of random numbers for the order,
and then to reorder them using a four loop.
So I'm going to go with this version and let's try it out.
Instead of printing password_list,
let's print the password_list before the shuffle,
and then let's use the random.shuffle and pass in the password_list.
And then let's print the outcome of that shuffle,
so let's print the password_list again.
So now you can see that we've got a version before,
and then we've got a version after.
So if I run my code and I write again, four letters,
two symbols, two numbers,
you can see that previously it was ordered in the order that we added each of
the characters. After the shuffle
they're now pretty much random. There's letters in random places,
there's symbols in random places.
So if only we could turn this back into a string,
then we've got our password that's nicely shuffled.
So how can we turn it back into a string? Well,
we could simply use a for loop, right?
We could say for each, um,
character in the password_list,
we're going to add it to a variable called a password,
which is going to start off again as a empty string
and then we're going to add to it.
So we're going to say password += char in our password_list.
And now if I go ahead and just print my final password,
I'm going to add a fstring as well.
Let's say your password is,
and then let's insert the password.
And now if I run my code and let's say,
I want 12 letters, uh,
three symbols and four numbers,
then it's going to generate me a entire password
that's completely randomized in terms of the orders of the symbols and the
numbers.
And I've now solved the hard part of this challenge project.
Did that make sense to you? Did you manage to get it
to do what you want it to do?
If you're at all confused about the code that we've covered here,
then be sure to check out the final project that I've got here,
which is the App generator end,
and take a look at how the easy level is implemented,
how the hard level is implemented and try switching it around so that you really
get to understand it.
Try instead of maybe getting the user to tell you how many letters,
how many symbols, how many numbers, instead just define those yourself
or use a random number of letters, numbers,
and symbols. Play around with the code until it starts doing what you want it to
do and it starts making sense.
But I hope you had fun building this project with me today and learning all
about loops. On the next lesson
we're going to be learning more about these things that we've been using a lot,
which is Python functions. So I'm going to bid you goodnight for today,
and hopefully I'll see you bright and early tomorrow,
and we'll learn some new skills and work on some new projects.
So that's all from me, goodnight from Angela.