Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Now that we've learned all about loops in Python,
2
we're finally ready to tackle our final project of the day,
3
which is the PyPassword generator.
4
Our program will ask us how many letters would you like in your password?
5
So I will say I would like a long password, let's say 14.
6
How many symbols, 3, how many numbers, 4,
7
and it's going to generate me a password that is almost completely impossible to
8
crack by any hacker, no matter how talented they are.
9
So the project of today is to build this password generator.
10
If you head over to this particular Repl.it,
11
password-generator-start, then you'll see that here
12
I've already included for you a list of all the letters in the alphabet from A
13
to Z, including the uppercase and lowercase letters.
14
I've got a bunch of numbers and a bunch of symbols that are usually accepted by
15
websites in the passwords.
16
And I've also created the inputs that you saw when we did the demo.
17
So it's going to ask us how many letters, how many symbols, how many numbers,
18
and then you're going to turn all of that knowledge as well as using these
19
lists
20
and what you've learned about loops into the same password generator that you
21
saw in the final demo.
22
So have a play around with the final demo yourself,
23
and then have a think about how you might be able to achieve this goal of
24
creating a random password for the user based on the number of letters,
25
symbols, and numbers that they want.
26
Now there's two levels to this project.
27
You can either go for the easy version where all you have to do is generate your
28
password in sequence. So for example, if the user wanted four letters,
29
two symbols, and two numbers,
30
then it might just look like this four letters and then two symbols and then two
31
numbers. So they're in sequence right? First, you've got the letters,
32
then you've got the number of required symbols,
33
and then you've got the required numbers. So this is the easy level.
34
And then there is the hard level. And the hard level requires you to generate the
35
password in a completely random order.
36
So instead of having it being, letter symbol number,
37
it should be maybe letter and then a symbol and then a number,
38
then a letter, then a symbol and then a number.
39
But notice how this is completely random.
40
Every single time you generate it,
41
the order of where the symbols and where the numbers appear in your password are
42
completely random. So for example,
43
if you look at the demo version and I do the same thing,
44
four letters, two symbols,
45
two numbers, where the numbers and where the symbols occur are completely
46
unpredictable. And every single time I generate a new password,
47
it's going to switch it up again.
48
So that's the hard level. I recommend trying the easy level first
49
and then once you've managed to do it, then take yourself up to the hard level
50
if you think you're up to it. But remember that if your doing the hard level,
51
you might have to do a little quick Google around in order to figure out how to
52
do something that you might not have seen before.
53
But it's all part of the challenge and I hope you'll take it on with vigor.
54
So pause the video now and try to complete the final project.
55
Alright,
56
so let's first tackle the easy level where we're just generating a password
57
with the required number of letters and then the required number of symbols and
58
then the required number of numbers.
59
So let's go ahead and tackle this one first.
60
Now we know that we've got these lists which we can draw from,
61
and we know that we can use the random module to get hold of a random item from
62
the list. So all we have to do is first get four random letters.
63
So let's say that we had a password
64
which just started out as an empty string.
65
Then we can use a for loop to loop through all of the characters that we need to
66
create for our password. For example,
67
I might say for char, short for character, in the range of,
68
um,
69
from 1 to the number of letters that the user wanted.
70
So in this case, if the user wanted four letters,
71
then it will be a range from one to four, but not including four.
72
So it's actually from one to three,
73
which has only three characters. In order to modify this,
74
we have to do what we did always before,
75
which is just to add one to the end of our range.
76
So now we're going to create a range based on the number of letters that the
77
user wanted.
78
And I'm just going to go along with the assumption that the number of letters
79
equals four. Let's just pretend that's what the user will enter.
80
So then this range becomes 1, 2, 4 + 1 which is 5.
81
So the range is in fact going to be 1 to 4. For every single number in that
82
range from one to two to three to four,
83
I'm going to generate them a random letter,
84
and I'm gonna pick it out of this list of letters. To do that
85
I can either generate a random number and use it as the index or you might've
86
seen previously
87
I showed you the random.choice function. And inside this random.choice,
88
we can pass a sequence
89
which in our case is the letters list.
90
Then it will look through this list of letters and it will give us a random item from
91
that list.
92
So now that random letter is going to be something that we're going to need to
93
add to our password, right?
94
So we can set this equal to a random character.
95
And now let's go ahead and just print it out.
96
Right?
97
So let's say we want four letters in our password and we can type anything for
98
the other ones, but notice how when the for loop runs,
99
it's given us some random characters that it's picked out of our letters list.
100
So there's an a,
101
there's a capital P there's a d and there's a capital J. How can we add each of those
102
letters to our password? Well,
103
we can use the string concatenation that we learned very early on in Day 1.
104
We can say password equals the previous password
105
plus the random character. Of course,
106
the shortened version of this is just password+= random_char.
107
And now let's go ahead and instead of printing the random character,
108
let's print the password at each stage of the loop.
109
Right?
110
And again,
111
I'm going to choose four letters and you can see that the first time the loop
112
runs I get an M. So password is equal to M. The second time I get a Q
113
so I add the Q onto the M, I get MQ and so on and so forth until I get these four
114
characters in the beginning of what seems to be my random password that's going
115
to be generated.
116
Right
117
Now, if you wanted to,
118
you can actually simplify this down to just one step,
119
because instead of having this intermediary,
120
which is the random character variable,
121
we can simply just replace it with the code, like this.
122
So in this case, we're getting a random choice from our letters list,
123
so we get a random letter, and then we add that to our password.
124
Now that we've done that for the letter,
125
then it's pretty easy to see how we can continue doing this for the symbol and
126
the numbers. We just have to keep adding it to the end of our password.
127
So let me quickly write out the code for this.
128
Right?
129
So now when the user requests that they want four letters in their password,
130
then we generate a range from one to five.
131
And then for each of those positions,
132
we generate a random letter in order to fill the password.
133
And then we add on the required number of symbols and the required number of
134
numbers. And finally, we print out the final password.
135
Let's go ahead and run this code. And again,
136
I'm going to choose four letters, two numbers, and two symbols.
137
And I end up with my final password,
138
which fits exactly that criteria and it looks pretty random.
139
But the only problem is that if a hacker knew that you always have two symbols
140
at this position, and two number at this position,
141
then it's very easy to crack that password because in order to get the last
142
digit, for example, they only have to try nine different variations, 12
143
3456789.
144
And this just limits the strength of your password. In order to tackle that you
145
would have to tackle it at the hard level,
146
which is how do you get a password that has all of these letters and numbers in
147
a completely random order. To do that
148
we mentioned that you have to do a bit of googling because it might require
149
something that you haven't done before. But in essence,
150
you've got access to everything you need to perform the logic.
151
So if you haven't given that a go,
152
then this might be a good time to pause and have a think about how you might
153
structure the logic or how you might create a flow chart to express this logic
154
that you would want. And once you've done that,
155
and if you want to see the solution and walk through it with me,
156
then come back here and I'll show you how to do it, but first give it a go.
157
Right?
158
All right. In order to tackle the hard level,
159
what we essentially need to do is instead of adding each of these letters into a
160
string, we actually want to add it into a list.
161
So I'm going to copy all of this that's here,
162
and I'm just gonna comment it out and I'm going to paste it under my hard level and
163
work on it here. So instead of using a password string,
164
I'm going to create a list and to better describe it
165
I'll change my variable name, the password list. So now,
166
instead of using password to add each of these letters and numbers and symbols,
167
I'm going to be adding it to my list.
168
Right,
169
Right,
170
Right
171
Now, to add things to a list,
172
you can use the same syntax that you did with strings,
173
which is to add the new item into the list and to update the old list.
174
You might be more familiar though with the syntax
175
which is append and both of these work.
176
You can use either of them if you want, but they'll end up doing the same thing,
177
which is to add each of those things you've generated in the order that you've
178
generated to your password.
179
So we end up now with a list with four letters,
180
two symbols, and two numbers,
181
all stored as strings because of the quotation marks around them.
182
So now that we've got a list, well, then we have to think about, well,
183
how can we shuffle our list,
184
how can we mess up the order of the items in our list?
185
And that might take you to Google and you might search for something like how to
186
change the order of items in a list.
187
And then we also have to add our keyword, which is Python,
188
because there's obviously a lot of other programming languages.
189
And we come up against our first Stack Overflow question,
190
how can I reorder a list? Well, that sounds kind of like what I want to do,
191
right? So if we scroll down,
192
you can see that the way to do it is either using a for loop,
193
like so, or
194
to simply use the shuffle function.
195
This is a much simpler way because it allows us to update the existing list just
196
by calling the shuffle function on it.
197
And this one actually requires us to create a new list and we have to create a
198
bunch of random numbers for the order,
199
and then to reorder them using a four loop.
200
So I'm going to go with this version and let's try it out.
201
Instead of printing password_list,
202
let's print the password_list before the shuffle,
203
and then let's use the random.shuffle and pass in the password_list.
204
And then let's print the outcome of that shuffle,
205
so let's print the password_list again.
206
So now you can see that we've got a version before,
207
and then we've got a version after.
208
So if I run my code and I write again, four letters,
209
two symbols, two numbers,
210
you can see that previously it was ordered in the order that we added each of
211
the characters. After the shuffle
212
they're now pretty much random. There's letters in random places,
213
there's symbols in random places.
214
So if only we could turn this back into a string,
215
then we've got our password that's nicely shuffled.
216
So how can we turn it back into a string? Well,
217
we could simply use a for loop, right?
218
We could say for each, um,
219
character in the password_list,
220
we're going to add it to a variable called a password,
221
which is going to start off again as a empty string
222
and then we're going to add to it.
223
So we're going to say password += char in our password_list.
224
And now if I go ahead and just print my final password,
225
I'm going to add a fstring as well.
226
Let's say your password is,
227
and then let's insert the password.
228
And now if I run my code and let's say,
229
I want 12 letters, uh,
230
three symbols and four numbers,
231
then it's going to generate me a entire password
232
that's completely randomized in terms of the orders of the symbols and the
233
numbers.
234
And I've now solved the hard part of this challenge project.
235
Did that make sense to you? Did you manage to get it
236
to do what you want it to do?
237
If you're at all confused about the code that we've covered here,
238
then be sure to check out the final project that I've got here,
239
which is the App generator end,
240
and take a look at how the easy level is implemented,
241
how the hard level is implemented and try switching it around so that you really
242
get to understand it.
243
Try instead of maybe getting the user to tell you how many letters,
244
how many symbols, how many numbers, instead just define those yourself
245
or use a random number of letters, numbers,
246
and symbols. Play around with the code until it starts doing what you want it to
247
do and it starts making sense.
248
But I hope you had fun building this project with me today and learning all
249
about loops. On the next lesson
250
we're going to be learning more about these things that we've been using a lot,
251
which is Python functions. So I'm going to bid you goodnight for today,
252
and hopefully I'll see you bright and early tomorrow,
253
and we'll learn some new skills and work on some new projects.
254
So that's all from me, goodnight from Angela.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.