Python Exercise | Dictionary Login System in Python | Beginner Level | With Challenges Application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back my fellow python programmers to a brand new video and in this one we're going to be building a dictionary log in system as a python exercise and we'll be covering the following topics so if at any point you feel that you're enjoying the contents of this video please make sure to smash that like button because I need to be the YouTube algorithm and let's go ahead and get started okay so this is going to be the login system program guide again I'm not going to go through this many times uh hopefully you're not new to this channel basically this is the guide you can follow with this guide step by step try to do this on your own and then watch me or you can simply go and code alongside with me you can also start on your own and then if you get stuck somewhere you can go and watch where I uh got to and then continue from there etc etc so first of all you'll need to create a dictionary in which the keys are the usernames and the values are the password so this is your first St step to working with dictionaries if you don't know how dictionaries work you can always go back to the video where I explain them I'm going to link it somewhere right here at the top so the keys are the usernames the values are the passwords you do your own uh I would recommend that you create two or three usernames and one admin and keep it simple just keep the usernames user one user two the passwords also keep them simple we need those to test things out next you'll need to create a function in which the user must enter a username and a password in order to sign in so this is going to be the login system function so two inputs the username and the password if they match then indicate that the user has successfully logged in have a simple print out very simple otherwise refuse the user's entry and finally test it out by calling the function and logging in using the usernames and passwords that you've created so pause the video right now and go try the program guide okay so as you can see right here we've created our user credentials dictionary first how did we know that this is a dictionary well because you can see that we have these two squiggly brackets around but that is not all this can also be a set a set also has the squiggly brackets around it so we also need to look inside of our dictionary right here and we can clearly see that we have key value Pairs and how are these key value pairs separated they are separated by a column something to keep in mind as you're writing these users and you're adding these keys and value pairs you'll notice that if I have two user two right here I get an error well not an error this is actually a warning and this says that the dictionary contains duplicate Keys user two you cannot have two keys that are the same inside of a dictionary it won't cause any issues but it's not how the dictionaries work what would happen in this case if you call user two let's say we have a 420 which is kind of funny but anyways if we do have user two right here with a pass 456 and a user two with a pass 420 then if we try to access user two like for example right here as you can see when I right click and I run I get pass for 20 and not pass 456 so some something very important to keep in mind when working with dictionaries okay so in here we've created a new or we've defined a new function called the log in system using the def keyword something that we've covered a long time ago this function will allow the user to log in their credentials and first we prompt the user to enter the username using the input and then we prompt them to enter the password using the input function and this is pretty straightforward nothing too much in here I'm simply going to remove the second user okay so now in here we are checking if the credential that the user entered are correct so we validate these credentials using two check the first one verifies whether the username that they entered is actually part of the user's credential and the way we do this is using the in this step ensures that the username provided by the user actually matches one that is registered in the system when using the in comparison between a username and the user credential basically what we're doing is we're taking the username value and going through every single key and checking if it is in indeed part of the users credential dictionaries this is very similar to similar to how we go through lists now the second condition right here compares the password associated with the given username in the dictionary to the password entered by the user and then we access the password using the key in between the square brackets so keep in mind when we're working with an end we need both of these conditions to be true if both conditions are met which means that the username exists and the entered password matches the stored password then the function proceeds to execute the code inside of the if statement so again we need both conditions to be true in order to enter if the username exists which means that this part of the condition is true but the username or the password at that user name is not equal to the password that the user entered then it will not work because this part of the condition or this side of the end will be false so we need both of them to be true otherwise we are going to get a failed login and now the last thing that we need to do is to Simply call in this login system in order to call the function so when I right click and I run boom there we go I'm prompted to enter the username let's just try a bad username with a bad password boom login failed let's try to run this again and I'm going to enter uh let's try user one boom and then I try pass 456 keeping mind this is user one so pass 456 log in failed let's try one last time with a correct user one and the password one 123 hit enter boom you have successfully logged in okay fellow python programmers congratulations on finishing this very simple exercise it was great because you learned how you can check if a key is in a dictionary the way we did the with the usernames and you also now know how to access values using those specific keys where we were checking for the password on that certain username but now it's time for a bit of a challenge where you need to complete this entire login system with a signup and just a general good main so let's go ahead and continue on okay so now this is going to be your challenge to sign me up create two functions sign up system and the main to choose whether to sign in or sign up this is for the main of course so first of all let's talk about the sign up function you'll need to ask the user to input their new username if the username already exists then they are prompted to enter another you username with indication meaning that you need to tell them oh you need a new username and you need to keep doing this until they provide a unique username so I can think of a good way of doing this until they provide a good username next you are prompt you prompt the user to input a password for their new username and add the new user to the user's dictionary so this is going to be the signup function oh and also of course you need to test it out by adding some usernames now you're going to go to the main function ask the user if they would like to log in or if they would like to sign up of course they have the option and based on that option you call one of the functions based on the input call call either of the functions and then try logging in the new added users from the signup function so once you complete the sign up signup function and you know that it works I want you to use that signup function to add a few more users and then try to log them in through the main function so pause the video right now and go do the challenge okay so we begin by defining a new function that is the signup system uh as you can see I use the error right here just to take down the login system because we don't need to change anything about it and I simply have a small print out just so we can differentiate between the logging in and the signup system if you want you can also add a line right here in the login system but that is for you to decide okay so in here we create a y Loop to continuously prompt the user for a usern name until they choose one that isn't already taken and the way we do this this is something that I told you about or I gave you a small hint of in the challenge slide we use a while true Loop so the user is asked to choose a username in this case case once they are once they input something into the prompt we get the new username variable we then check if this new username already exists in the user credentials dictionary if the username is found then we print out that this username is already taken please choose another one but if the username is not found then we simply break out of our function all or break out out break out of our Loop using of course the the break allowing the user then to move on to the next step which is going to be inputting a password and of course the password can be anything because the password can be unique to every user and you might have two similar passwords okay and now it's time for the final step in our signup system and that is to update the user's credential dictionary with this new key value pair and the way we do this is by simply accessing the user credential dictionary with the new username and because this new username does not already exist in the dictionary then a new key is going to be created with the key being the new username and then we be assigned the value new password and that way we create a new user in our dictionary and finally we also print out sign up successfully you can now log in with your new account now let's continue on to the main function okay so this is going to be our main function as you can see nothing too much in here a simple while true Loop because we want to keep asking because I want to log in and sign up several times so we can test this out but usually what you would do is you would simply have a break point or two breaks right here once they choose to log in and once they choose or once they choose to sign up then after you call the function you simply break out of this entire Loop but because we're testing actually you know what I'm just going to there we go I'm going to comment them out and let's continue on uh we get the action in here so I called it action this can also be a user input or action or whatever you want uh then we have the input function do you want to log in or you want to sign up and then we tell them that they should either write log in or sign up and then I put it to down to lower do lower which if I hover over this it gives me returns a copy of the string converted to the lowercase why because someone might be entering on a keyboard where they put the first letter or uh first letter in the uppercase so we want to take it down to the lower so that we can then compare it right here and you'll see that this is something that's very common if you want to compare strings because maybe somebody doesn't just have the first key on the uppercase they might have G in the uppercase and remember when comparing strings in Python capital letters are different than lowercase letters so we take in the input we lower it down in case there are any uppercase letters and then we check if the action which is the input is equal to the login then we call the login system L if we call the signup system else if the user just enters something random we tell them please enter login or sign up maybe this should be like that and boom should be working so let's go ahead and call this function and test it out so instead of the login system I'm going to say main right here and when I right click and I run boom there we go do you want to log in or do you want to sign up so for example if I type in something random hit enter please enter login or sign up so login boom enter the username user two and then the password pass uh 456 was it yes you have successfully logged in excellent now let's go ahead and try and sign up so I'm going to sign up boom what's your new account I'm going to say that it's uh Michael lowercase Michael the password will be 123 hit enter sign up successful you can now log in with your new account let's try logging in with Michael with a password of one to three boom you have successfully logged in how cool is that so we were able to add to our dictionary as well as go through its values and go through everything so one more thing I would like to do in here is I would actually like to print out my uh dictionary once we've actually signed up so after signing up sign up successful I'm simply going to print this out and it will look very ugly but this is just for testing so I'm going to print out the user credentials right click and run you want to log in or do you want want to log in or want to sign up uh you do you want to log in so okay there we go I was like wait I did not put that that's not really like professional so anyways uh log sign up boom sign up to your new account choose your username again Michael incorrectly written Michael uh 159 hit enter successfully signed up so as you can see we have user one password 1 2 3 user 2 pass 456 admin and finally we have Michael let's try signing up again and in here we can have kin and then 456 hit enter boom as you can see we have Kalin and if we try to sign in oh sign in I mean login you know what probably calling it uh sign in would have been more coherent with the sign up but anyways who cares kin if we try kin let's try a wrong password like 66 hit enter login failed incorrect username or password boom there we go so we have to login kayin 456 and now you have successfully logged in all right fellow python programmers congratulations on making it this far we've gone through a lot of things when it comes to dictionaries we've understood how we can check if a certain key is already available to us in our dictionary we also were able to access values or access the value at a certain key and we also learned how we can add new entries with new keys and new values into our already existing dictionary so we've gone through through a few things so now it's time for you to continue on your Python Programming Journey with a new exercise which I don't know what will that be right here I just can yes I can see you have enough space for the thumbnail of the next video you should watch continue your Python Programming Journey let's go new exercise let's go leave a like And subscribe nice let's go
Info
Channel: 2M Python Dev
Views: 306
Rating: undefined out of 5
Keywords:
Id: evNOzLfdo6U
Channel Id: undefined
Length: 17min 42sec (1062 seconds)
Published: Tue Mar 19 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.