Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,450 --> 00:00:03,210
Now that we've learned all about loops in Python,
2
00:00:03,540 --> 00:00:06,960
we're finally ready to tackle our final project of the day,
3
00:00:07,290 --> 00:00:09,690
which is the PyPassword generator.
4
00:00:10,440 --> 00:00:14,100
Our program will ask us how many letters would you like in your password?
5
00:00:14,100 --> 00:00:17,640
So I will say I would like a long password, let's say 14.
6
00:00:18,300 --> 00:00:22,290
How many symbols, 3, how many numbers, 4,
7
00:00:22,950 --> 00:00:27,390
and it's going to generate me a password that is almost completely impossible to
8
00:00:27,390 --> 00:00:30,840
crack by any hacker, no matter how talented they are.
9
00:00:31,620 --> 00:00:35,640
So the project of today is to build this password generator.
10
00:00:36,180 --> 00:00:38,670
If you head over to this particular Repl.it,
11
00:00:38,670 --> 00:00:43,320
password-generator-start, then you'll see that here
12
00:00:43,320 --> 00:00:48,320
I've already included for you a list of all the letters in the alphabet from A
13
00:00:48,360 --> 00:00:51,630
to Z, including the uppercase and lowercase letters.
14
00:00:52,200 --> 00:00:56,850
I've got a bunch of numbers and a bunch of symbols that are usually accepted by
15
00:00:56,850 --> 00:00:58,260
websites in the passwords.
16
00:00:59,040 --> 00:01:03,330
And I've also created the inputs that you saw when we did the demo.
17
00:01:03,870 --> 00:01:07,440
So it's going to ask us how many letters, how many symbols, how many numbers,
18
00:01:07,920 --> 00:01:12,270
and then you're going to turn all of that knowledge as well as using these
19
00:01:12,270 --> 00:01:13,080
lists
20
00:01:13,080 --> 00:01:18,060
and what you've learned about loops into the same password generator that you
21
00:01:18,060 --> 00:01:19,890
saw in the final demo.
22
00:01:20,340 --> 00:01:23,160
So have a play around with the final demo yourself,
23
00:01:23,700 --> 00:01:27,900
and then have a think about how you might be able to achieve this goal of
24
00:01:27,900 --> 00:01:32,900
creating a random password for the user based on the number of letters,
25
00:01:33,330 --> 00:01:35,670
symbols, and numbers that they want.
26
00:01:37,050 --> 00:01:40,110
Now there's two levels to this project.
27
00:01:40,200 --> 00:01:45,200
You can either go for the easy version where all you have to do is generate your
28
00:01:46,680 --> 00:01:51,180
password in sequence. So for example, if the user wanted four letters,
29
00:01:51,540 --> 00:01:53,250
two symbols, and two numbers,
30
00:01:53,550 --> 00:01:58,550
then it might just look like this four letters and then two symbols and then two
31
00:02:01,080 --> 00:02:04,650
numbers. So they're in sequence right? First, you've got the letters,
32
00:02:04,950 --> 00:02:07,110
then you've got the number of required symbols,
33
00:02:07,260 --> 00:02:11,490
and then you've got the required numbers. So this is the easy level.
34
00:02:12,060 --> 00:02:17,060
And then there is the hard level. And the hard level requires you to generate the
35
00:02:18,330 --> 00:02:21,810
password in a completely random order.
36
00:02:21,840 --> 00:02:25,500
So instead of having it being, letter symbol number,
37
00:02:25,800 --> 00:02:30,800
it should be maybe letter and then a symbol and then a number,
38
00:02:30,810 --> 00:02:33,930
then a letter, then a symbol and then a number.
39
00:02:34,980 --> 00:02:37,950
But notice how this is completely random.
40
00:02:38,310 --> 00:02:40,020
Every single time you generate it,
41
00:02:40,110 --> 00:02:45,110
the order of where the symbols and where the numbers appear in your password are
42
00:02:45,450 --> 00:02:47,430
completely random. So for example,
43
00:02:47,430 --> 00:02:51,210
if you look at the demo version and I do the same thing,
44
00:02:51,210 --> 00:02:53,190
four letters, two symbols,
45
00:02:53,190 --> 00:02:58,110
two numbers, where the numbers and where the symbols occur are completely
46
00:02:58,110 --> 00:03:02,500
unpredictable. And every single time I generate a new password,
47
00:03:02,770 --> 00:03:04,240
it's going to switch it up again.
48
00:03:04,360 --> 00:03:09,220
So that's the hard level. I recommend trying the easy level first
49
00:03:09,430 --> 00:03:13,660
and then once you've managed to do it, then take yourself up to the hard level
50
00:03:13,690 --> 00:03:17,710
if you think you're up to it. But remember that if your doing the hard level,
51
00:03:17,770 --> 00:03:22,450
you might have to do a little quick Google around in order to figure out how to
52
00:03:22,450 --> 00:03:24,850
do something that you might not have seen before.
53
00:03:25,330 --> 00:03:29,560
But it's all part of the challenge and I hope you'll take it on with vigor.
54
00:03:29,710 --> 00:03:33,670
So pause the video now and try to complete the final project.
55
00:03:42,580 --> 00:03:42,870
Alright,
56
00:03:42,870 --> 00:03:47,730
so let's first tackle the easy level where we're just generating a password
57
00:03:48,090 --> 00:03:52,680
with the required number of letters and then the required number of symbols and
58
00:03:52,680 --> 00:03:54,540
then the required number of numbers.
59
00:03:55,050 --> 00:03:57,060
So let's go ahead and tackle this one first.
60
00:03:57,810 --> 00:04:01,680
Now we know that we've got these lists which we can draw from,
61
00:04:01,890 --> 00:04:06,660
and we know that we can use the random module to get hold of a random item from
62
00:04:06,660 --> 00:04:11,190
the list. So all we have to do is first get four random letters.
63
00:04:11,610 --> 00:04:14,010
So let's say that we had a password
64
00:04:14,040 --> 00:04:16,770
which just started out as an empty string.
65
00:04:17,430 --> 00:04:22,430
Then we can use a for loop to loop through all of the characters that we need to
66
00:04:23,580 --> 00:04:27,030
create for our password. For example,
67
00:04:27,030 --> 00:04:32,030
I might say for char, short for character, in the range of,
68
00:04:32,850 --> 00:04:33,360
um,
69
00:04:33,360 --> 00:04:38,100
from 1 to the number of letters that the user wanted.
70
00:04:41,010 --> 00:04:43,380
So in this case, if the user wanted four letters,
71
00:04:43,440 --> 00:04:48,240
then it will be a range from one to four, but not including four.
72
00:04:48,660 --> 00:04:50,820
So it's actually from one to three,
73
00:04:51,180 --> 00:04:54,240
which has only three characters. In order to modify this,
74
00:04:54,270 --> 00:04:56,430
we have to do what we did always before,
75
00:04:56,430 --> 00:04:59,730
which is just to add one to the end of our range.
76
00:05:00,240 --> 00:05:04,620
So now we're going to create a range based on the number of letters that the
77
00:05:04,620 --> 00:05:05,460
user wanted.
78
00:05:05,610 --> 00:05:08,640
And I'm just going to go along with the assumption that the number of letters
79
00:05:08,670 --> 00:05:11,910
equals four. Let's just pretend that's what the user will enter.
80
00:05:12,420 --> 00:05:16,200
So then this range becomes 1, 2, 4 + 1 which is 5.
81
00:05:16,470 --> 00:05:21,470
So the range is in fact going to be 1 to 4. For every single number in that
82
00:05:22,620 --> 00:05:25,050
range from one to two to three to four,
83
00:05:25,350 --> 00:05:28,200
I'm going to generate them a random letter,
84
00:05:28,260 --> 00:05:32,010
and I'm gonna pick it out of this list of letters. To do that
85
00:05:32,190 --> 00:05:37,190
I can either generate a random number and use it as the index or you might've
86
00:05:37,830 --> 00:05:38,910
seen previously
87
00:05:38,970 --> 00:05:43,970
I showed you the random.choice function. And inside this random.choice,
88
00:05:45,450 --> 00:05:47,610
we can pass a sequence
89
00:05:47,640 --> 00:05:50,760
which in our case is the letters list.
90
00:05:51,450 --> 00:05:56,450
Then it will look through this list of letters and it will give us a random item from
91
00:05:58,490 --> 00:05:59,323
that list.
92
00:05:59,780 --> 00:06:04,340
So now that random letter is going to be something that we're going to need to
93
00:06:04,340 --> 00:06:06,320
add to our password, right?
94
00:06:06,740 --> 00:06:11,210
So we can set this equal to a random character.
95
00:06:12,140 --> 00:06:14,570
And now let's go ahead and just print it out.
96
00:06:15,490 --> 00:06:16,323
Right?
97
00:06:19,690 --> 00:06:24,690
So let's say we want four letters in our password and we can type anything for
98
00:06:24,940 --> 00:06:27,910
the other ones, but notice how when the for loop runs,
99
00:06:28,000 --> 00:06:33,000
it's given us some random characters that it's picked out of our letters list.
100
00:06:34,540 --> 00:06:35,470
So there's an a,
101
00:06:35,500 --> 00:06:40,500
there's a capital P there's a d and there's a capital J. How can we add each of those
102
00:06:41,050 --> 00:06:43,900
letters to our password? Well,
103
00:06:43,900 --> 00:06:48,900
we can use the string concatenation that we learned very early on in Day 1.
104
00:06:49,720 --> 00:06:53,380
We can say password equals the previous password
105
00:06:53,710 --> 00:06:57,460
plus the random character. Of course,
106
00:06:57,460 --> 00:07:01,960
the shortened version of this is just password+= random_char.
107
00:07:02,440 --> 00:07:06,070
And now let's go ahead and instead of printing the random character,
108
00:07:06,400 --> 00:07:09,610
let's print the password at each stage of the loop.
109
00:07:14,050 --> 00:07:14,290
Right?
110
00:07:14,290 --> 00:07:14,830
And again,
111
00:07:14,830 --> 00:07:19,750
I'm going to choose four letters and you can see that the first time the loop
112
00:07:19,750 --> 00:07:24,700
runs I get an M. So password is equal to M. The second time I get a Q
113
00:07:24,700 --> 00:07:29,700
so I add the Q onto the M, I get MQ and so on and so forth until I get these four
114
00:07:30,100 --> 00:07:35,100
characters in the beginning of what seems to be my random password that's going
115
00:07:35,530 --> 00:07:36,490
to be generated.
116
00:07:38,340 --> 00:07:38,540
Right
117
00:07:38,540 --> 00:07:39,680
Now, if you wanted to,
118
00:07:39,680 --> 00:07:43,550
you can actually simplify this down to just one step,
119
00:07:43,850 --> 00:07:46,160
because instead of having this intermediary,
120
00:07:46,160 --> 00:07:48,140
which is the random character variable,
121
00:07:48,410 --> 00:07:53,060
we can simply just replace it with the code, like this.
122
00:07:53,330 --> 00:07:57,470
So in this case, we're getting a random choice from our letters list,
123
00:07:57,560 --> 00:08:01,490
so we get a random letter, and then we add that to our password.
124
00:08:02,330 --> 00:08:04,730
Now that we've done that for the letter,
125
00:08:04,910 --> 00:08:09,020
then it's pretty easy to see how we can continue doing this for the symbol and
126
00:08:09,020 --> 00:08:12,620
the numbers. We just have to keep adding it to the end of our password.
127
00:08:12,980 --> 00:08:15,140
So let me quickly write out the code for this.
128
00:08:23,510 --> 00:08:23,770
Right?
129
00:08:23,770 --> 00:08:27,880
So now when the user requests that they want four letters in their password,
130
00:08:28,240 --> 00:08:31,510
then we generate a range from one to five.
131
00:08:31,900 --> 00:08:34,570
And then for each of those positions,
132
00:08:34,600 --> 00:08:38,650
we generate a random letter in order to fill the password.
133
00:08:39,190 --> 00:08:42,730
And then we add on the required number of symbols and the required number of
134
00:08:42,730 --> 00:08:46,600
numbers. And finally, we print out the final password.
135
00:08:47,110 --> 00:08:50,830
Let's go ahead and run this code. And again,
136
00:08:50,830 --> 00:08:54,310
I'm going to choose four letters, two numbers, and two symbols.
137
00:08:54,990 --> 00:08:57,780
And I end up with my final password,
138
00:08:58,050 --> 00:09:01,920
which fits exactly that criteria and it looks pretty random.
139
00:09:02,670 --> 00:09:07,670
But the only problem is that if a hacker knew that you always have two symbols
140
00:09:08,190 --> 00:09:11,310
at this position, and two number at this position,
141
00:09:11,640 --> 00:09:16,640
then it's very easy to crack that password because in order to get the last
142
00:09:16,650 --> 00:09:20,940
digit, for example, they only have to try nine different variations, 12
143
00:09:20,940 --> 00:09:22,920
3456789.
144
00:09:23,400 --> 00:09:28,400
And this just limits the strength of your password. In order to tackle that you
145
00:09:29,460 --> 00:09:31,620
would have to tackle it at the hard level,
146
00:09:31,890 --> 00:09:36,660
which is how do you get a password that has all of these letters and numbers in
147
00:09:36,660 --> 00:09:40,500
a completely random order. To do that
148
00:09:40,650 --> 00:09:43,680
we mentioned that you have to do a bit of googling because it might require
149
00:09:43,680 --> 00:09:46,950
something that you haven't done before. But in essence,
150
00:09:47,010 --> 00:09:50,940
you've got access to everything you need to perform the logic.
151
00:09:51,480 --> 00:09:53,280
So if you haven't given that a go,
152
00:09:53,520 --> 00:09:57,210
then this might be a good time to pause and have a think about how you might
153
00:09:57,210 --> 00:10:02,210
structure the logic or how you might create a flow chart to express this logic
154
00:10:03,060 --> 00:10:05,730
that you would want. And once you've done that,
155
00:10:05,820 --> 00:10:08,490
and if you want to see the solution and walk through it with me,
156
00:10:08,670 --> 00:10:13,230
then come back here and I'll show you how to do it, but first give it a go.
157
00:10:15,870 --> 00:10:16,703
Right?
158
00:10:17,110 --> 00:10:19,720
All right. In order to tackle the hard level,
159
00:10:20,050 --> 00:10:25,050
what we essentially need to do is instead of adding each of these letters into a
160
00:10:25,300 --> 00:10:29,050
string, we actually want to add it into a list.
161
00:10:30,040 --> 00:10:32,410
So I'm going to copy all of this that's here,
162
00:10:32,500 --> 00:10:37,500
and I'm just gonna comment it out and I'm going to paste it under my hard level and
163
00:10:37,720 --> 00:10:42,250
work on it here. So instead of using a password string,
164
00:10:42,430 --> 00:10:45,280
I'm going to create a list and to better describe it
165
00:10:45,730 --> 00:10:49,660
I'll change my variable name, the password list. So now,
166
00:10:49,660 --> 00:10:54,660
instead of using password to add each of these letters and numbers and symbols,
167
00:10:55,990 --> 00:10:58,240
I'm going to be adding it to my list.
168
00:10:59,650 --> 00:11:00,420
Right,
169
00:11:00,420 --> 00:11:01,253
Right,
170
00:11:02,210 --> 00:11:03,000
Right
171
00:11:03,000 --> 00:11:05,130
Now, to add things to a list,
172
00:11:05,340 --> 00:11:09,240
you can use the same syntax that you did with strings,
173
00:11:09,660 --> 00:11:14,660
which is to add the new item into the list and to update the old list.
174
00:11:17,430 --> 00:11:20,340
You might be more familiar though with the syntax
175
00:11:20,340 --> 00:11:23,850
which is append and both of these work.
176
00:11:23,940 --> 00:11:28,110
You can use either of them if you want, but they'll end up doing the same thing,
177
00:11:28,170 --> 00:11:32,580
which is to add each of those things you've generated in the order that you've
178
00:11:32,580 --> 00:11:34,560
generated to your password.
179
00:11:34,800 --> 00:11:39,120
So we end up now with a list with four letters,
180
00:11:39,510 --> 00:11:42,030
two symbols, and two numbers,
181
00:11:42,210 --> 00:11:46,650
all stored as strings because of the quotation marks around them.
182
00:11:47,460 --> 00:11:51,360
So now that we've got a list, well, then we have to think about, well,
183
00:11:51,570 --> 00:11:53,800
how can we shuffle our list,
184
00:11:53,800 --> 00:11:56,890
how can we mess up the order of the items in our list?
185
00:11:57,730 --> 00:12:01,420
And that might take you to Google and you might search for something like how to
186
00:12:01,420 --> 00:12:05,410
change the order of items in a list.
187
00:12:05,680 --> 00:12:09,160
And then we also have to add our keyword, which is Python,
188
00:12:09,190 --> 00:12:12,490
because there's obviously a lot of other programming languages.
189
00:12:12,910 --> 00:12:15,910
And we come up against our first Stack Overflow question,
190
00:12:15,910 --> 00:12:19,930
how can I reorder a list? Well, that sounds kind of like what I want to do,
191
00:12:19,930 --> 00:12:22,450
right? So if we scroll down,
192
00:12:22,510 --> 00:12:27,510
you can see that the way to do it is either using a for loop,
193
00:12:27,910 --> 00:12:30,430
like so, or
194
00:12:30,700 --> 00:12:33,520
to simply use the shuffle function.
195
00:12:34,630 --> 00:12:39,630
This is a much simpler way because it allows us to update the existing list just
196
00:12:41,740 --> 00:12:43,990
by calling the shuffle function on it.
197
00:12:44,650 --> 00:12:49,510
And this one actually requires us to create a new list and we have to create a
198
00:12:49,510 --> 00:12:52,270
bunch of random numbers for the order,
199
00:12:52,600 --> 00:12:54,910
and then to reorder them using a four loop.
200
00:12:55,300 --> 00:12:58,840
So I'm going to go with this version and let's try it out.
201
00:12:59,350 --> 00:13:01,420
Instead of printing password_list,
202
00:13:01,480 --> 00:13:04,270
let's print the password_list before the shuffle,
203
00:13:04,600 --> 00:13:09,600
and then let's use the random.shuffle and pass in the password_list.
204
00:13:10,150 --> 00:13:13,480
And then let's print the outcome of that shuffle,
205
00:13:13,480 --> 00:13:15,640
so let's print the password_list again.
206
00:13:17,740 --> 00:13:20,380
So now you can see that we've got a version before,
207
00:13:20,590 --> 00:13:22,210
and then we've got a version after.
208
00:13:22,420 --> 00:13:26,920
So if I run my code and I write again, four letters,
209
00:13:26,980 --> 00:13:29,020
two symbols, two numbers,
210
00:13:29,590 --> 00:13:34,210
you can see that previously it was ordered in the order that we added each of
211
00:13:34,210 --> 00:13:36,580
the characters. After the shuffle
212
00:13:36,610 --> 00:13:40,570
they're now pretty much random. There's letters in random places,
213
00:13:40,600 --> 00:13:42,160
there's symbols in random places.
214
00:13:42,550 --> 00:13:45,970
So if only we could turn this back into a string,
215
00:13:46,450 --> 00:13:49,870
then we've got our password that's nicely shuffled.
216
00:13:50,260 --> 00:13:53,290
So how can we turn it back into a string? Well,
217
00:13:53,290 --> 00:13:55,720
we could simply use a for loop, right?
218
00:13:55,810 --> 00:13:59,080
We could say for each, um,
219
00:13:59,440 --> 00:14:03,910
character in the password_list,
220
00:14:04,690 --> 00:14:08,980
we're going to add it to a variable called a password,
221
00:14:09,370 --> 00:14:12,610
which is going to start off again as a empty string
222
00:14:13,240 --> 00:14:15,430
and then we're going to add to it.
223
00:14:15,460 --> 00:14:20,440
So we're going to say password += char in our password_list.
224
00:14:21,040 --> 00:14:24,790
And now if I go ahead and just print my final password,
225
00:14:25,180 --> 00:14:27,070
I'm going to add a fstring as well.
226
00:14:27,550 --> 00:14:30,310
Let's say your password is,
227
00:14:31,360 --> 00:14:33,460
and then let's insert the password.
228
00:14:34,840 --> 00:14:38,230
And now if I run my code and let's say,
229
00:14:38,230 --> 00:14:40,900
I want 12 letters, uh,
230
00:14:40,930 --> 00:14:43,330
three symbols and four numbers,
231
00:14:43,870 --> 00:14:47,530
then it's going to generate me a entire password
232
00:14:47,740 --> 00:14:52,550
that's completely randomized in terms of the orders of the symbols and the
233
00:14:52,550 --> 00:14:53,383
numbers.
234
00:14:53,660 --> 00:14:58,660
And I've now solved the hard part of this challenge project.
235
00:15:00,080 --> 00:15:03,530
Did that make sense to you? Did you manage to get it
236
00:15:03,530 --> 00:15:04,940
to do what you want it to do?
237
00:15:06,800 --> 00:15:10,070
If you're at all confused about the code that we've covered here,
238
00:15:10,370 --> 00:15:14,510
then be sure to check out the final project that I've got here,
239
00:15:14,510 --> 00:15:16,700
which is the App generator end,
240
00:15:17,150 --> 00:15:20,600
and take a look at how the easy level is implemented,
241
00:15:20,630 --> 00:15:25,160
how the hard level is implemented and try switching it around so that you really
242
00:15:25,160 --> 00:15:26,390
get to understand it.
243
00:15:26,720 --> 00:15:31,070
Try instead of maybe getting the user to tell you how many letters,
244
00:15:31,070 --> 00:15:34,970
how many symbols, how many numbers, instead just define those yourself
245
00:15:35,000 --> 00:15:37,790
or use a random number of letters, numbers,
246
00:15:37,790 --> 00:15:42,440
and symbols. Play around with the code until it starts doing what you want it to
247
00:15:42,440 --> 00:15:44,240
do and it starts making sense.
248
00:15:44,840 --> 00:15:48,770
But I hope you had fun building this project with me today and learning all
249
00:15:48,770 --> 00:15:51,440
about loops. On the next lesson
250
00:15:51,440 --> 00:15:54,800
we're going to be learning more about these things that we've been using a lot,
251
00:15:55,160 --> 00:16:00,110
which is Python functions. So I'm going to bid you goodnight for today,
252
00:16:00,200 --> 00:16:03,200
and hopefully I'll see you bright and early tomorrow,
253
00:16:03,680 --> 00:16:06,860
and we'll learn some new skills and work on some new projects.
254
00:16:07,070 --> 00:16:09,710
So that's all from me, goodnight from Angela.
24322
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.