Project Based Python: Username and Password

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to project based python these tutorials will not cover python basics in depth if you need help with the basics you can try pi learning companion it is a python study app that covers all the core concepts and short lessons then test your skills with hundreds of activities pi learning companion is available on android and ios you can find the links in the video description today we will create a program that prompts the user to create a username and password we will use functions lists and loops the username will be checked against the list of other users to make sure that it is unique then we will check that the username has no numbers in it and that it is between 8 and 14 characters long we will then create a password that has at least one number one capital letter and one special character we will then make sure that the password is eight characters or longer let's get started first we need to create some variables that we're going to use in our program let's start with a list called users and we will have three names in this list so let's start with sam and frank and let's say jane so when the user creates their username they will not be able to use one of these three names it will have to be something else then we're going to create a list of capital letters so that we can make sure that the password uh contains one of these letters now we could do this by saying caps and then creating a list and in the list we can pass in each individual character this would take some time so there's actually a faster way to do this but we need to import something from the python library so we're going to say import string and now instead of having instead of passing each individual letter we can simply say string dot ascii and we can choose between uppercase letters and lowercase we only need the uppercase letters right now so we will say string ascii uppercase let's print the caps just so we can see what that looks like so print caps and down here we see we get every single letter of the alphabet and they're all capitals now let's remove this print statement we don't really need it anymore and create a new list we'll call this one numbers and we'll create an empty list then populate it with a for loop so for i in range and we'll pass in 10 here so that we can get number zero through nine once we get these numbers we want to add them to the to the numbers list and convert them to strings so that it is a little bit easier for us later so we're going to say numbers dot append str so we're converting the i from integer to a string now numbers is going to be a range of numbers from 0 to 9 and they will all be in string format let's print that out to see what it looks like so print numbers and run it and we get 0 1 2 3 and so on let's remove this print statement now and let's create two more variables that we're going to use so the next one will be another list and we'll call this one special characters i'll just abbreviate it and we will use this list to make sure that the user password has one of these special characters so let's add one more here we can do dollar sign and we need one more variable called name check we'll set this one to false and this this name check will allow the user to proceed once the username is accepted so we'll first get the username and make sure that everything is okay and then we can move on to creating a password now we can start creating the functions that are going to perform the checks and the first one will check whether or not the username is in this list so we will call this one name available we need to pass in the name and then use an if statement to perform the check so if name dot title and i am using title to make sure that whatever the user enters so for example if the user enters john it will turn it into [Music] a capital j and then the rest will be lower case the reason why i'm doing that is if for example we typed in sam with a lowercase s it would count as a different name from the one that we have in here so i just want to make sure that we're not getting a duplicate and so if name.title in users return false so that means that the name is not available otherwise return true now let's make sure that our function actually works so i will print name available and i will pass in sam so this one should be false because we have sam in our list then i will print name available and i'll pass in a lowercase sam so that should also be false because here we're using name.title then i will print name available and let's go with jeff so this one is not in our list and it should return true because the name is available let's try that so we have false for sam falls for the second sam and we have true for jeff now we can move on to our next function so let me delete these print statements get rid of this and now we're going to check for numbers in the name so the function will be called number found we need to pass in the name and we're going to use the list of numbers that we've created up here to loop through each individual character of the name and make sure that none of those characters match the numbers so we will say first we'll create a variable here called found and say false we will then later return this variable but first we need to perform the check so for number in numbers if name dot find number is greater than negative one now what this does uh name.find and then whatever we're passing in here will take this character and look for it in the name if the character is found it returns a number that is the index value of that character now that index value can be anything from 0 to however long the name is but if the number if the character is not found it will return negative one so here we're checking whether or not uh the return value here is larger than negative one if it is it means it is found otherwise it is not so here we need to set found to equal true and then we need to return found so what this will do is it will loop through the name make sure that there are no numbers in it and if it does find a number we'll return a true so number is found if it doesn't find the number it will return false so we will know that no numbers were found in the name and let's try this out so i can say print number number found and i will pass in a name so i'll just say gym so this one should return false and then i will print number found and let's say gym 74 that one should return as true and meaning that there was a number found in the name so if i run this we get false for the first one because there are no numbers in the name and then in the second one we have true so there is a number right here it sets true found to true and we have a number in our name now we can create our while loop to prompt the user to enter a username and then perform some of these checks so while true we will get the username input and let's give a prompt of enter your username now we need to perform the first check which is going to check whether or not the username is in the list so we're going to use the name available function so name available and now we need to pass in the username so if the name is available for now we will just pass and the reason why i'm using pass is that the if statement will be invalid if there is nothing down here so i'm just going to use pass and then handle the else statement first so i will say else and i will print please choose another username all right so that means that we've performed the check against the list up here and if we found find a double we will tell the user to use another username so let's try this we can say sam because we know that's in the list and we get please choose another username if i type in jimmy we get enter your username so we're just going into the pass here which will handle next let's stop this and here we need to perform the next check so if not number found so we don't want to have a number in the name we need to pass the username and we'll use the pass again here and handle the else statement and then we can just say print username cannot contain numbers so now if we run this we can try sam we get please choose another username if i say jimmy 5 we get username cannot contain numbers but if we just type in jimmy everything works we just get into this pass statement now let's stop this and perform one last check for the username so here we need to check the length of the username so if 2 is smaller than len username and that is smaller than 15. so this is taking this is making sure that 2 is smaller than whatever the length of the username is and that the length of the username is smaller than 15 so that will give us a username that is between 3 and 14 characters long now we can say name check equals true and this will allow the user to move on past this part so we'll say name check equals true and users dot append username so that will just add our username to that list to the list up here and we will also give the user a pro a message to say print username create all right so this should work now if we run it we can try you know we have jane that is in the list so please choose another name we can say thread three username cannot contain numbers but if we just use fred we get username created now we can move on to creating the password but before we do that we need to put this inside of an if statement so if not name check we need to perform these checks on the name and then once name check is true we can move on to the next part so we'll say else and let's get the password from the user so password equals input enter your password now we need to create a function for the first password check and the first function will actually check whether or not the password has capital letters in it so we need to create a function and we'll call it caps found we need to pass on the password and this one will look similar to the numbers found so found equals false or letter in caps so we're taking each individual capital letter that we have in that list that we created we will check if password dot find letter is greater than negative one found will equal true and then we need to return found so these two look almost identical let's try using it so here we will say if caps found and pass in the password let's just pass that for now and create an else statement and in the else statement we will print a password must contain a capital letter let's test that out so enter your username let's say freddy and then enter your password so i have no capital letters we get password must contain a capital letter let's have a capital letter and then a bunch of other letters and we get inside of the if statement here so we get to the pass now we can move on to the next check and this one will require another function so we will create one here def special found and pass in the password found equals false and then four character in special characters if password dot find character is greater than negative one we will set found to be true and then we just need to return found okay now we can use that down here we are going to say if special found and pass in the password let's just say pass right now handle the else statement and in the else statement we'll say print password must contain a special character and we'll just remind the user which special characters we're talking about so we'll say exclamation mark at sign pound sign or a dollar sign all right let's test it out we can say jimmy and enter your password we will just use one without a capital letter it says you must contain a capital letter then let's have a capital letter then it says password must contain a special character and let's have a correct one now so w something something something at and there it is now we are here again now we will check that the password contains at least one number so instead of pass here we will say if number found and remember we're using the function that we've created up here so we can just reuse this one and instead of passing in the name we're passing in the password if this returns true it means that there is a number in our password so if number found password and again we will pass and handle it with an else statement here we'll print password must contain a number so let's check this part ready and let's just do e some letters a special character but no number and we get password must contain a number so now let's do r something pound sign and a two and now we are back to the pass we can now close this and perform the final check on the password so now we need to make sure that the password is at least eight characters long so we are going to say if len password is greater than seven so if it is eight or more characters pass for now let's handle the else we will say print password must be at least eight characters long let's try that so jimmy and then let's have a capital letter some other letters a special character a number but it is not eight characters long so we get password must be at least eight characters long let's try it again so k something special character and a number it is more than 8 characters this time and we get back to the pass so let's close this again and instead of the pass we should give the user a final message and in this final message we can actually print out the username and the password that they have created and then we also need to make sure that we break out of that loop that we created up here so i will get rid of pass and we'll use a formatted string and we will say your user name is use the username here and your password is and type in the password now username is highlighted here and if you hover over it it will say username can be undefined it will never be undefined in our code because we make sure that we have a username before we move on to the password for this to not be highlighted we could actually create the username outside of the while loop so just set it to an empty string up here and then change it later but we really don't need to do that then finally let's just break out of the loop and now let's create a valid username and password so i will say jimmy for my password i will say q 3 3 something something and then the letter d so oh and a special character and if i hit enter we have your username is jimmy and your password is whatever i just typed in if you have enjoyed this tutorial make sure to like and subscribe also check out pi learning companion the link is in the video description you can also find the source code below
Info
Channel: TMD Studios
Views: 810
Rating: undefined out of 5
Keywords: python, tutorial, projectbasedpython, project, coding, programming, username and password, project based python
Id: G_XYXuC8b9M
Channel Id: undefined
Length: 20min 32sec (1232 seconds)
Published: Sun Nov 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.