So in the series of learning Python programming language, we have discussed one loop, four loop in Python, right? Now we'll be working on one project in this video. This is our second project in the series. One project we have discussed that was rock, paper, scissors game, right? Very simple.
This is also not a tough one, but you have to use whatever knowledge you have gained. till now in the series you have to use all your knowledge here in this project and the project is you have to create you can say it's a password generator program right now okay before the discussing this thing let me just show you what kind of output should be there for this project right okay so this kind of output I want like first it will show you welcome to the password generator that line or it is not necessary the next line is it will ask how many letters would you like in your password let us suppose I am entering four letters I want then how many symbols would you like three how many numbers you like like suppose two and enter see nothing this is my password. In this password I am having like four letters, small e, capital s, small n, small n, then symbols, three symbols, star. this m percent and then this round bracket then two numbers I have 9 and 9. Randomly it will choose any number maybe same number it can choose.
Here in this case it is choosing the same number 9 and 9 two numbers. It may be 7 9 or 1 2 or any numbers right. So this kind of password. This one is what?
The hard level password tough one. easy level maybe like four letters so at starting it will give four letters then three symbols then three numbers let me just show you that output also okay the simple one is same it will ask how many letters would you like in your password suppose four then how many symbols? Three and numbers suppose two numbers.
See four numbers sorry four letters so it's starting it will continue give four letters then three symbols then continue three symbols then two numbers and then two numbers. These are not it's like you can know these are not in a shuffled way. 4 letters, 10 symbols, 10 numbers.
So if hacker knows at which position you have 2 numbers, at what position you have symbols, at what position you have letters, then it would be easy for him to crack the password, right? So you will be working on both the levels. First one, the easy one, this one, and the next level is the hard level. Your password should be shuffled, right? So let's try this out now.
Okay, now you go to what kind of house? output we want right for this password generator project right now first one two levels are there easy level and hard level so first try with easy one right so obviously see we you have to generate like random numbers random numbers random symbols like three numbers or four numbers or whatever number a user will enter that much numbers right so one idea is you have to understand first you the logic don't just you know go to your laptop and try to you know type the program and all first understand or maybe if it is possible try to make a flow chart so random number you have to generate so obviously we will use some random kind of things so you have to import random module so read about this thing from random what you can use which many functions are there what function is you know suitable for you to this for to create this project right now you have like list of letters small abcd capital abcd list of numbers from 0 to 9 list of symbols some symbols are there you can take that list or whatever you want i don't know you can take those things you can do some random functions you use of some random functions how you will choose and suppose if you are choosing like obviously four four letters first one letter then second then third and fourth like this so you will use loop for loop we have discussed we have discussed range function also that also you can use here and that would be i guess if you are able to put together all these things in correct way then it would be very easy for you guys right so try this out pause the video and try this out first try this that easy level then go to the hard level okay then i'll we'll discuss the solution so i hope you have done this project if not it's okay you can go to the solution or maybe take some hint and pause the video and then try the try this out let's create a new file and we'll be naming this project too underscore password generator dot py so obviously we'll be using some random functionalities random functions so better to import random module first right then we'll be having from letters we'll be choosing from capital or small letters 26 capital 26 small then some numbers then symbols so let's take list of those letters symbols and numbers okay so here here i have letters the list small abcd capital abcd their numbers from 0 to 9 and symbols these are three list i'm having so randomly we will be choosing from these list right so first thing is what it will print like welcome to password generator and then it will ask from the user how many numbers you want how many letters and how many symbols so using input function we'll be taking input Like in input we simply write how many letters you want in your password and that thing always storing in a variable. Letters we cannot take letters because it is name of this list also.
So number of letters n underscore letters and whatever you will take sorry using input function that would be a string but I want that should be. integer so let's typecast this here only int okay same line we will write for like symbols and for numbers so i'm just copying and pasting this line here and we'll do some changes till now we are fine right now we have to generate letters symbols and numbers from the list right And then first generate suppose I want four letters. So four letters and then we can like we can take an empty string and add those four letters to that empty string. Then suppose three symbols. So then three symbols.
Then add suppose two numbers. Then add two numbers to that empty to that string and then just simply print that string. That's it. That is easy level.
So let's take a empty string first, name is password and this is what an empty string I am taking, right? Now I want to generate 4, suppose 4 letters, I am entering at first input 4, 4 letters, 1, 2, 3, 4. So we have to use a for loop like for a variable name variable name i'm taking suppose i or care or a b c d x y z any variable you can take for i in now do we have a function that can that can generate these like numbers like i want four letters so it can generate like four numbers one two three four if seven then seven one two three four five six seven something like this yes we have a range function range and we can give the range one two 1, 2, this time I want 4 letters, right? So, if I give like 1, 2 rather than obviously 4, second time maybe I user enter 5 letters, 6 letters, 7 letters.
So, obviously statically I cannot give. So, here what? This variable we will be taking n underscore letters.
But the problem is if 1 to 4, if I am giving n letters is equal to. 4. So range function will generate 1, 2, 3, not 4 including 4, not 4 but I want 4 letters 1, 2, 3, 4 right. If you are not aware about this you can just check out the video on range function we have discussed.
If the n letters is 5 so it will generate 1, 2, 3, 4 whatever the end size the stop point stop point minus 1 it will generate. So, if you want to generate these 4 letters, if we are giving this input 4, then you have to give here n letters plus 1. So, if input I am giving 4, so here 1, 2, 4 plus 1, 1, 2, 5, then it will generate 1, 2, 3, 4. Yeah, exactly I want 4 letters. So, it's okay 1, 2, 3, 4, right?
Now, at this point of at running time, this rather than this, it will be replaced with 1, 2, 3, 4. So, at first 1 would be assigned to this i, 1, 1 in this range 1, 2, 3, 4, yes. condition true. So now we will generate a random, any random letter from the list. So if you want to generate any random letter from the list we have a function choice.
If you remember that random module that video. Random dot choice and in bracket simply pass that list name. List name is letters.
This letters. right? And whatever letter it will produce, it will generate, we will fetch, we will you know fetch that letter or just store that letter in a variable suppose char or any variable we can take. This time I am taking, here I am taking char, right?
And then after generating that character just add that character to this password, this empty string. So, just add this thing here, password is equal to or you can say concatenate and simply we have discussed in the very first videos of my Python series using plus we can concatenate in strings. So, password plus char that's it, right?
So, if you simply want to print at this point of time. What would be the password after generating, after just completing one loop only, the numbers only. Just we are going to just check out like till now we are working fine or not or is there any problem or is there any error. Let's run this.
Welcome to password generator. How many letters you want? Suppose 4. It will ask how many symbols? 3. How many numbers?
So four letters are there. Small o, capital m, small u, capital z and it will just print these four letters. That's it. Now you have to generate three symbols, two numbers and add in the string and just print. I hope now this is easy for you at this point of time.
If you have got whatever, if you have got that one for loop, right? So If you have got this for loop then easily you can write down those two remaining for loops, right? And rather than this, this line we can use that shorthand also, the password plus equal to char, that is also fine, right?
Or rather than these two lines we can simply use, we can simply write password plus equal to random dot choice letters, in one line also we can write down this thing. But better to write down, you know for better understanding. For beginners.
Now for beginners it would be better to write down these two lines, right? Okay, rather than printing the password here, now one more for loop for i in range, one more for loop for second thing is for symbols. So range would be 1 2 n underscore symbols plus 1, why plus 1?
I hope you got this. And then here same we will generate a character sorry like any random symbol. So we have a variable random sorry it's r a n d 1 random dot choice and simply pass symbols from this list we have to choose right. And then simply we have a password we can simply add. that character, those characters one by one, right?
So here for symbols also one loop is for numbers. So simply I am going to copy paste this loop only for i in range, third is for numbers. So it should be n numbers plus one, then rather than the list name is here should be numbers and then we will be adding this. one by one this character to the password and finally we will print now print password okay let's run this and see letters how many letters 4 3 2 see at starting we have four letters then we have three symbols symbols may be same two symbols are same like the opening braces opening round right and two numbers 8 2 but these are not shuffled. These are in a particular order.
So this is not a tough one to crack. This password is easy. Now, if you haven't done this before, at this point of time, you got the idea, right?
Now pause the video and try the hard level without checking the solution, right? Okay, now I hope you have done this. What we can do, see. If you have, you remember that function shuffle, we have discussed a shuffle is a function in a random module, choice is to choose any particular, any random thing from, any random item from the list and shuffle is to shuffle the list.
So suppose rather than this string, because at first here we are taking password as a string rather than the string, if we have a list. password list, here we have list, then we can easily shuffle that list using shuffle and then we can simply print. Good idea? So rather than taking here string, let's take list, let's take another thing, password list and rather than string we are taking, sorry, we are taking empty list here, right? Now, Maybe you are thinking in using plus we can concatenate in string.
Same method applies to a list also. You can just generate any random character and you can using plus only you can simply concatenate to that list also. So no need to change here. Plus.
That is fine. Three loops would be same. Right? So okay.
Rather than here print password at this point of time we are printing password list. Okay. Let me just show you the difference.
Okay, 4, 3, 2. Password is not defined. Okay, sorry, we are using here password but the name is password list at this time we have updated, we have changed the name, it's password list. Here also password list and here also we have password list, right.
Okay, I hope it works 4, 3, 2. See, now we will be having 4 letters, 3 symbols, 2 numbers but in a form of list. Okay, now next, next thing is shuffle this list. Okay, simply we have, using for loop also we can shuffle this, 2 methods.
But directly we are using, we have a inbuilt function so use that one shuffle and password list okay and now again print password list okay let's run this and see see this is first and this list is after shuffling see here it's not like that first four we are having letters first we are having this letter q, then we are having a symbol, then again a letter, then a number, then letter like this. So we have shuffled this list. So this is I guess more secure password.
Now it's a list obviously but we need password in a form of a string just. So how to convert this thing into a string? Okay fine.
We will simply take a empty string, very easy, right? We will take a for loop, we will be taking a for loop for i or any variable name you can take for i in password list, in that list, shuffled password list. So, now password list is.
And then from the shuffled password list, the first item would be assigned to this i. So that item in password list that would be obviously true. So we will fetch that first item and then add that item to this empty string.
Yes? Okay. So how to add this password plus equal to or rather than i if we take care.
then that would be more meaningful. Password plus equal to char that's written, after that just print this string password, right away. Let me show you.
Let's run this 4, 3, 2. See, first without shuffling, then the list after shuffling, then we have this password. So rather than printing this list and this list, you can simply print this, this thing. So I guess this password is a tough one to crack, right? See, without shuffling in our output, this was the list after shuffling. this was the list right and then we have taken an empty string then for char in password list now at this point of time this is password list so first first of all the first item would be taken 8 now 8 would be assigned to this char this 8 in password list yes condition true so control will enter here and password plus equal to char now password is equal password is now empty so in empty string we'll be adding the character in character we have 8 so first of all eight would be added then next is three then three would be added then this address of that would be added then you then dollar till the end of the list all the character one by one would be added to this password and we simply print that password the string now i hope you got the working of this thing both the level both the level you go easy one and hard one if not it's okay pause the video take the notes with you know on your paper dryer on this thing and then definitely we'll get it right so i hope you go teach in every line of the program we have written to generate this thing for a password generator project right if not please don't skip the video and don't just jump like you have you have you know watched my video and you got this no maybe you can just copy paste the complete code on your you know with you know pen and on your paper and Just dry run that thing.
Try to understand each and every line. Why we have written that line in the program? What is the meaning of that line?
Try to understand the logic. Don't just go with my words or my program. Dry run at your own.
So I think you got this project. You have done this project. Now we are done with two projects in the series. So now I will see you in the next video.
Till then bye bye. Take care.