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.
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.
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 4 letters, 2 symbols,
2 numbers, where the numbers and where the symbols occur are completely unpredictable.
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, pause the video now and try to complete the final project.
All right, 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...from 1 to the number of
letters that the user wanted.
So in this case, if the user wanted 4 letters, then it will be a range from 1 to 4, but not including
4.
So it's actually from 1 to 3, which is 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 4.
Let's just pretend that's what the user will enter.
So then this range becomes 1 - 4 + 1 which is 5.
So the range is in fact going to be 1 - 4. For every single number in that range from 1 to
2 to 3 to 4, I'm going to generate them a random letter.
And I'm going to 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 have 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 (random.char).
And now let's go ahead and just print it out.
So let's say we want 4 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.
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 the password equals the previous password plus the random_char
Of course, the shortened version of this is just password += random_char.
And now let's go ahead,
and instead of printing the random_char, let's print the password at each stage of the loop.
And again I'm going to choose 4 letters.
So you can see the first time the for loop runs I get a capital H as the random_char, and then
the next time the loop runs, I get an A and the A is tagged to the end of the H.
So I get HA, 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.
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_char 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.
So now when the user requests that they want four letters in their password, then we generate a range
from 1 to 5.
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.
Now there's a way to simplify our code even more.
Previously, we always used the range by starting from one and then going up to one over the number
that we want.
For example, when we wanted 1 to 100, we created a range of 1 to 101.
Now in this case, because all that we want is a range that covers all the letters, symbols, and numbers
that the user wants.
So if the user wanted four letters, then what we need the range to be is 1, 2, 3, and 4,
which in this case is a range of (1, 5),
because of the way that the range doesn't include the upper limit.
Now, if we think about this logically, we can also change this to 0, 1, 2, 3
So then we can have a range of 0 to 4.
So that means we can actually use the number of letters as the upper limit,
and then simply change this to 0 so that we don't have to add 1 to our number of letters.
And I think this will make our code even simpler and hopefully simpler to understand as well.
Now, if you prefer the previous version, which we've seen throughout the course, then feel free to
keep it as it is.
But this way we get rid of all of these plus ones and you can see it's a lot tidier.
Let's go ahead and run this code.
And again I'm going to choose 4 letters, 2 numbers, and 2 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 numbers 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 123456789
.
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.
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 going to 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 to 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.
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 StackOverflow 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 for loop.
So I'm going to go with this version and let's try it out.
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 4 letters, 2 symbols, 2 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 are 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 char in the password_list..."we're going to add it to a variable called
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 += each character in our password_list.
And now if I go ahead and just print my final password I'm going to add an f-string 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, 3 symbols and 4 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 wanted 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 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 good night 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.
Good night from Angela.