WEBVTT

0
00:00.260 --> 00:06.470
Now that we've learned all about loops in Python, we're finally ready to tackle our final project of

1
00:06.470 --> 00:09.890
the day, which is the PyPassword Generator.

2
00:10.160 --> 00:14.060
Our program will ask us, "How many letters would you like in your password?"

3
00:14.060 --> 00:16.640
So I will say I would like a long password.

4
00:16.640 --> 00:17.900
Let's say 14.

5
00:18.020 --> 00:19.400
"How many symbols?"

6
00:19.940 --> 00:20.570
3.

7
00:20.600 --> 00:21.740
"How many numbers?"

8
00:21.740 --> 00:22.550
4.

9
00:22.610 --> 00:29.370
And it's going to generate me a password that is almost completely impossible to crack by any hacker,

10
00:29.370 --> 00:31.140
no matter how talented they are.

11
00:31.320 --> 00:35.790
So the project of today is to build this password generator.

12
00:36.150 --> 00:42.600
Then you'll see that here, I've already included for you a list of all the letters in the alphabet, from

13
00:42.600 --> 00:46.290
A to Z, including the uppercase and lowercase letters.

14
00:46.350 --> 00:52.920
I've got a bunch of numbers and a bunch of symbols that are usually accepted by websites in the passwords.

15
00:53.350 --> 00:56.710
Now there's two levels to this project.

16
00:56.710 --> 01:04.930
You can either go for the Easy Version, where all you have to do is generate your password in sequence.

17
01:04.930 --> 01:10.930
So for example, if the user wanted four letters, two symbols and two numbers, then it might just

18
01:10.930 --> 01:18.290
look like this four letters and then two symbols and then two numbers.

19
01:18.290 --> 01:19.880
So they're in sequence, right?

20
01:19.880 --> 01:24.410
First you've got the letters, then you've got the number of required symbols, and then you've got

21
01:24.410 --> 01:26.240
the required numbers.

22
01:26.240 --> 01:28.340
So this is the Easy Level.

23
01:28.340 --> 01:30.980
And then there is the Hard Level.

24
01:31.310 --> 01:38.360
And the Hard Level requires you to generate the password in a completely random order.

25
01:38.360 --> 01:46.410
So instead of having it being letter, symbol, number, it should be maybe letter and then a symbol, and

26
01:46.410 --> 01:50.790
then a number, then a letter, then a symbol and then a number.

27
01:51.360 --> 01:59.340
But notice how this is completely random every single time you generate it, the order of where the

28
01:59.340 --> 02:03.180
symbols and where the numbers appear in your password are completely random.

29
02:03.180 --> 02:09.730
So for example, if you look at the demo version, and I do the same thing 4 letters, 2 symbols,

30
02:09.730 --> 02:15.520
2 numbers, where the numbers and where the symbols occur are completely unpredictable.

31
02:15.520 --> 02:17.740
So that's the Hard Level.

32
02:17.740 --> 02:23.770
I recommend trying the Easy Level first, and then once you've managed to do it, then take yourself

33
02:23.770 --> 02:24.850
up to the Hard Level.

34
02:24.850 --> 02:29.230
If you think you're up to it, pause the video now and try to complete the final project.

35
02:37.820 --> 02:44.480
All right, so let's first tackle the Easy Level where we're just generating a password with the required

36
02:44.480 --> 02:45.530
number of letters,

37
02:45.530 --> 02:50.180
and then the required number of symbols, and then the required number of numbers.

38
02:50.180 --> 02:52.730
So let's go ahead and tackle this one first.

39
02:52.920 --> 02:57.150
Now we know that we've got these lists which we can draw from,

40
02:57.150 --> 03:02.640
and we know that we can use the Random Module to get hold of a random item from the list.

41
03:02.640 --> 03:06.780
So all we have to do is first get four random letters.

42
03:06.780 --> 03:12.450
So let's say that we had a password which just started out as an empty string,

43
03:12.570 --> 03:21.540
then we can use a for loop to loop through all of the characters that we need to create for our password.

44
03:21.540 --> 03:31.620
For example, I might say for char, short for character, in the range of...from 1 to the number of

45
03:31.620 --> 03:33.720
letters that the user wanted.

46
03:36.150 --> 03:43.330
So in this case, if the user wanted 4 letters, then it will be a range from 1 to 4, but not including

47
03:43.330 --> 03:43.810
4.

48
03:43.810 --> 03:48.250
So it's actually from 1 to 3, which is only three characters.

49
03:48.250 --> 03:53.770
In order to modify this, we have to do what we did always before, which is just to add one to the

50
03:53.770 --> 03:55.360
end of our range.

51
03:55.360 --> 04:00.880
So now we're going to create a range based on the number of letters that the user wanted.

52
04:00.880 --> 04:04.960
And I'm just going to go along with the assumption that the number of letters equals 4.

53
04:04.960 --> 04:07.550
Let's just pretend that's what the user will enter.

54
04:07.550 --> 04:11.690
So then this range becomes 1 - 4 + 1 which is 5. 

55
04:11.690 --> 04:19.120
So the range is in fact going to be 1 - 4. For every single number in that range from 1 to

56
04:19.120 --> 04:23.600
2 to 3 to 4, I'm going to generate them a random letter.

57
04:23.600 --> 04:26.420
And I'm going to pick it out of this list of letters.

58
04:26.420 --> 04:32.040
To do that I can either generate a random number and use it as the index,

59
04:32.040 --> 04:40.320
or you might have seen previously, I showed you the random.choice() function. And inside this random.choice(),

60
04:40.320 --> 04:46.410
we can pass a sequence which in our case is the letters list.

61
04:46.530 --> 04:54.730
Then it will look through this list of letters and it will give us a random item from that list.

62
04:54.880 --> 05:01.330
So now that random letter is going to be something that we're going to need to add to our password,

63
05:01.330 --> 05:01.900
right?

64
05:01.900 --> 05:06.880
So we can set this equal to a random character (random.char).

65
05:07.150 --> 05:10.270
And now let's go ahead and just print it out.

66
05:14.830 --> 05:21.140
So let's say we want 4 letters in our password, and we can type anything for the other ones.

67
05:21.140 --> 05:28.040
But notice how when the for loop runs, it's given us some random characters that it's picked out of

68
05:28.040 --> 05:29.570
our letters list.

69
05:29.600 --> 05:33.770
How can we add each of those letters to our password?

70
05:33.830 --> 05:40.040
Well, we can use the string concatenation that we learned very early on in Day 1.

71
05:40.040 --> 05:46.950
We can say the password equals the previous password plus the random_char

72
05:47.280 --> 05:52.680
Of course, the shortened version of this is just password += random_char.

73
05:52.680 --> 05:54.150
And now let's go ahead,

74
05:54.150 --> 06:00.330
and instead of printing the random_char, let's print the password at each stage of the loop.

75
06:04.530 --> 06:07.660
And again I'm going to choose 4 letters.

76
06:08.080 --> 06:14.860
So you can see the first time the for loop runs I get a capital H as the random_char, and then

77
06:14.860 --> 06:20.500
the next time the loop runs, I get an A and the A is tagged to the end of the H.

78
06:20.500 --> 06:27.520
So I get HA, and so on and so forth until I get these four characters in the beginning of what seems

79
06:27.520 --> 06:31.430
to be my random password that's going to be generated.

80
06:32.990 --> 06:39.770
Now, if you wanted to, you can actually simplify this down to just one step, because instead of having

81
06:39.770 --> 06:45.950
this intermediary, which is the random_char variable, we can simply just replace it with the

82
06:45.950 --> 06:47.930
code like this.

83
06:47.930 --> 06:52.280
So in this case, we're getting a random choice from our letters list,

84
06:52.280 --> 06:56.490
so we get a random letter, and then we add that to our password.

85
06:56.910 --> 07:03.000
Now that we've done that for the letter, then it's pretty easy to see how we can continue doing this

86
07:03.000 --> 07:04.500
for the symbol and the numbers.

87
07:04.500 --> 07:07.560
We just have to keep adding it to the end of our password.

88
07:07.560 --> 07:10.080
So let me quickly write out the code for this.

89
07:18.310 --> 07:24.460
So now when the user requests that they want four letters in their password, then we generate a range

90
07:24.460 --> 07:26.500
from 1 to 5.

91
07:26.500 --> 07:33.670
And then for each of those positions, we generate a random letter in order to fill the password.

92
07:33.670 --> 07:38.260
And then we add on the required number of symbols and the required number of numbers.

93
07:38.260 --> 07:41.650
And finally we print out the final password.

94
07:43.460 --> 07:47.510
Now there's a way to simplify our code even more.

95
07:47.540 --> 07:56.000
Previously, we always used the range by starting from one and then going up to one over the number

96
07:56.000 --> 07:56.690
that we want.

97
07:56.720 --> 08:03.320
For example, when we wanted 1 to 100, we created a range of 1 to 101.

98
08:03.410 --> 08:12.120
Now in this case, because all that we want is a range that covers all the letters, symbols, and numbers

99
08:12.120 --> 08:13.410
that the user wants.

100
08:13.410 --> 08:23.160
So if the user wanted four letters, then what we need the range to be is 1, 2, 3, and 4,

101
08:23.160 --> 08:27.270
which in this case is a range of (1, 5),

102
08:27.270 --> 08:31.380
because of the way that the range doesn't include the upper limit.

103
08:31.410 --> 08:38.610
Now, if we think about this logically, we can also change this to 0, 1, 2, 3

104
08:38.610 --> 08:43.230
So then we can have a range of 0 to 4.

105
08:43.230 --> 08:48.390
So that means we can actually use the number of letters as the upper limit,

106
08:48.390 --> 08:55.240
and then simply change this to 0 so that we don't have to add 1 to our number of letters.

107
08:55.240 --> 09:02.110
And I think this will make our code even simpler and hopefully simpler to understand as well.

108
09:02.140 --> 09:07.660
Now, if you prefer the previous version, which we've seen throughout the course, then feel free to

109
09:07.660 --> 09:08.860
keep it as it is.

110
09:08.860 --> 09:14.530
But this way we get rid of all of these plus ones and you can see it's a lot tidier.

111
09:14.530 --> 09:16.900
Let's go ahead and run this code.

112
09:17.300 --> 09:22.040
And again I'm going to choose 4 letters, 2 numbers, and 2 symbols.

113
09:22.130 --> 09:27.770
And I end up with my final password which fits exactly that criteria,

114
09:27.770 --> 09:29.660
and it looks pretty random.

115
09:29.870 --> 09:37.190
But the only problem is that if a hacker knew that you always have two symbols at this position and

116
09:37.190 --> 09:43.590
two numbers at this position, then it's very easy to crack that password, because in order to get

117
09:43.590 --> 09:49.710
the last digit, for example, they only have to try nine different variations 123456789

118
09:49.710 --> 09:50.580
.

119
09:50.580 --> 09:54.870
And this just limits the strength of your password.

120
09:54.960 --> 10:00.630
In order to tackle that, you would have to tackle it at the Hard Level, which is how do you get a

121
10:00.630 --> 10:06.670
password that has all of these letters and numbers in a completely random order.

122
10:06.970 --> 10:11.980
To do that, we mentioned that you have to do a bit of Googling because it might require something that

123
10:11.980 --> 10:18.670
you haven't done before, but in essence, you've got access to everything you need to perform the logic.

124
10:18.670 --> 10:24.280
So if you haven't given that a go, then this might be a good time to pause and have a think about how

125
10:24.280 --> 10:30.890
you might structure the logic, or how you might create a flow chart to express this logic that you

126
10:30.890 --> 10:31.760
would want.

127
10:32.000 --> 10:36.620
And once you've done that, and if you want to see the solution and walk through it with me, then come

128
10:36.620 --> 10:39.410
back here, and I'll show you how to do it,

129
10:39.410 --> 10:40.940
but first give it a go.

130
10:44.390 --> 10:45.080
All right.

131
10:45.080 --> 10:51.230
In order to tackle the Hard Level, what we essentially need to do is instead of adding each of these

132
10:51.230 --> 10:56.700
letters into a string, we actually want to add it into a list.

133
10:57.270 --> 11:02.610
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

134
11:02.610 --> 11:06.480
to paste it under my Hard Level and work on it here.

135
11:06.540 --> 11:13.020
So instead of using a password string I'm going to create a list and to better describe it.

136
11:13.020 --> 11:15.930
I'll change my variable name to Password list.

137
11:16.050 --> 11:23.950
So now instead of using password to add each of these letters and numbers and symbols, I'm going to

138
11:23.950 --> 11:25.900
be adding it to my list.

139
11:30.250 --> 11:38.410
Now, to add things to a list, you can use the same syntax that you did with strings, which is to

140
11:38.410 --> 11:44.630
add the new item into the list and to update the old list.

141
11:44.660 --> 11:51.350
You might be more familiar though with the syntax, which is append(), and both of these work.

142
11:51.350 --> 11:57.200
You can use either of them if you want, but they'll end up doing the same thing, which is to add each

143
11:57.200 --> 12:02.120
of those things you've generated in the order that you've generated to your password.

144
12:02.120 --> 12:11.770
So we end up now with a list with four letters, two symbols, and two numbers, all stored as strings

145
12:11.770 --> 12:14.380
because of the quotation marks around them.

146
12:14.740 --> 12:21.280
So now that we've got a list, well, then we have to think about, well, how can we shuffle our list?

147
12:21.280 --> 12:24.580
How can we mess up the order of the items in our list?

148
12:25.090 --> 12:30.310
And that might take you to Google, and you might search for something like how to change the order

149
12:30.310 --> 12:32.950
of items in a list.

150
12:32.950 --> 12:38.860
And then we also have to add our keyword, which is Python, because there's obviously a lot of other

151
12:38.860 --> 12:40.150
programming languages.

152
12:40.150 --> 12:45.280
And we come up against our first StackOverflow question, how can I reorder a list?

153
12:45.310 --> 12:47.980
Well, that sounds kind of like what I want to do, right?

154
12:47.980 --> 12:58.370
So if we scroll down, you can see that the way to do it is either using a for loop like so, or to

155
12:58.370 --> 13:01.220
simply use the shuffle() function.

156
13:01.910 --> 13:10.760
This is a much simpler way, because it allows us to update the existing list just by calling the shuffle()

157
13:10.760 --> 13:17.360
function on it, and this one actually requires us to create a new list, and we have to create a bunch

158
13:17.360 --> 13:22.590
of random numbers for the order and then to reorder them using a for loop.

159
13:22.590 --> 13:26.550
So I'm going to go with this version and let's try it out.

160
13:26.550 --> 13:29.670
Let's print the password list before the shuffle.

161
13:29.670 --> 13:35.070
And then let's use the random.shuffle and pass in the password_list.

162
13:35.070 --> 13:38.760
And then let's print the outcome of that shuffle.

163
13:38.760 --> 13:41.100
So let's print the password_list again.

164
13:42.790 --> 13:45.670
So now you can see that we've got a version before.

165
13:45.670 --> 13:47.530
And then we've got a version after.

166
13:47.530 --> 13:56.170
So if I run my code and I write again 4 letters, 2 symbols, 2 numbers, you can see that previously

167
13:56.170 --> 14:01.870
it was ordered in the order that we added each of the characters, after the shuffle,

168
14:01.870 --> 14:03.850
they're now pretty much random.

169
14:03.850 --> 14:05.830
There's letters in random places,

170
14:05.830 --> 14:07.600
there's are symbols in random places,

171
14:07.600 --> 14:11.440
So if only we could turn this back into a string.

172
14:11.440 --> 14:15.280
Then we've got our password that's nicely shuffled.

173
14:15.280 --> 14:17.980
So how can we turn it back into a string?

174
14:17.980 --> 14:20.950
Well, we could simply use a for loop, right?

175
14:20.950 --> 14:33.560
We could say, "for each char in the password_list..."we're going to add it to a variable called

176
14:33.560 --> 14:38.150
password, which is going to start off again as a empty string.

177
14:38.150 --> 14:40.700
And then we're going to add to it.

178
14:40.700 --> 14:45.950
So we're going to say password  += each character in our password_list.

179
14:45.950 --> 14:52.580
And now if I go ahead and just print my final password I'm going to add an f-string as well.

180
14:52.700 --> 14:58.980
Let's say,  "Your password is:"...and then let's insert the {password}

181
14:59.790 --> 15:09.390
And now if I run my code and let's say I want 12 letters,  3 symbols and 4 numbers, then

182
15:09.390 --> 15:17.130
it's going to generate me a entire password that's completely randomized in terms of the orders of the

183
15:17.130 --> 15:18.640
symbols and the numbers.

184
15:18.640 --> 15:24.730
And I've now solved the hard part of this challenge project.

185
15:25.150 --> 15:27.100
Did that make sense to you?

186
15:27.130 --> 15:30.460
Did you manage to get it to do what you wanted to do?

187
15:31.780 --> 15:37.870
If you're at all confused about the code that we've covered here, then be sure to check out the final

188
15:37.870 --> 15:42.160
project and take a look at how the Easy Level is implemented,

189
15:42.160 --> 15:47.780
how the Hard Level is implemented, and try switching it around so that you really get to understand

190
15:47.780 --> 15:48.050
it.

191
15:48.050 --> 15:53.510
Try instead of maybe, getting the user to tell you how many letters, how many symbols, how many

192
15:53.510 --> 15:54.230
numbers,

193
15:54.230 --> 16:00.050
instead, just define those yourself or use a random number of letters, numbers, and symbols.

194
16:00.050 --> 16:05.900
Play around with the code until it starts doing what you want it to do, and it starts making sense.

195
16:06.180 --> 16:11.430
But I hope you had fun building this project with me today and learning all about loops.

196
16:11.670 --> 16:16.500
On the next lesson, we're going to be learning more about these things that we've been using a lot,

197
16:16.500 --> 16:19.020
which is Python functions.

198
16:19.020 --> 16:24.960
So I'm going to bid you good night for today, and hopefully I'll see you bright and early tomorrow,

199
16:24.960 --> 16:28.440
and we'll learn some new skills and work on some new projects.

200
16:28.440 --> 16:30.060
So that's all from me.

201
16:30.060 --> 16:31.470
Good night from Angela.