So the first thing we're going to do will be
to create the function that will register the user.
However, let us create a dictionary
that will be used to store the user credentials. So
for the dictionary, we can call it user underscore credentials.
All right equals and then we're going to have our co
braces.
All right.
Now for the actual function to register our user.
In fact, let me just add the note there real quick. So function to register,
use up. OK?
Let's define
the function. So I'm gonna go
there
and then let's say register underscore user.
OK? Colon.
Now what do we need? First of all, we need to ask the user to input the user name
and we can assign that to a variable, let's call that variable username
equals.
And now let us prompt the user to provide the username. So input
and then we can say enter
your username.
OK? Just as an example,
let's press enter
and let me leave some space in here between the colon and the
our code to have some space in.
So
right now we need to check to see if
the username that the user provides actually already exists.
So let me just add a note in here on a separate line.
Let's add the notes,
check to see if username
already
exists.
OK? So how are we going to do this? Well, we can do so
by using the if statement. So I'm gonna say if username and now in
the user credentials
again, Python has already given us the code.
So if username in the user credentials call on,
let us print username already exists, please choose a different username.
I think I like this. However, I'm going to remove the return, we don't need that.
So,
however, if the username
doesn't exist, if it's a brand new username, we can then add our else.
What else do we now need to do? We need to ask the user for their password, right? So
I'm gonna say let
us create a variable password
and then assign that to the inputs,
inputs and then in brackets,
let's say,
enter a
password
call on and then let's add the closing coats
and there it is. Ok.
So from here right now,
the next step that we'll have to do
is to create
the key value pair
for username and password. Now, how are we going to do this?
Well, we're going to do so
by calling the dictionary user credentials.
Ok. And now check this out. I'm gonna open up my brackets
and I'm going to provide the key which will be username
and now equals to password.
This is how we can create
the key value pair.
You provide the name of the dictionary and then in brackets,
you provide the name of the key equals to its value
and there it is right there. And then the final step will now be to simply print
registration
successful
and there it is.
OK.
Now let us try something. I am going to call this function
and let's see if it actually works. I'm going to say register user
brackets
colon
and there it is. Ok.
Let us try this.
Oh, so we don't need columns in here. I do apologize. We don't need colons there.
Let's run the program
all enter user name.
I'm going to say John,
let's enter a password. Let's just say John, ok, for the password.
Uh John
enter once again and it says registration successful.
Ok.
However, notice that the program has ended
and that's because we don't have an actual loop
that will continually run to keep asking the user to provide a new username,
a new password.
So
what can we do to
run this program indefinitely? Well,
we can introduce the while true loop. So over here
just underneath the register user function,
I am not going to call in a loop that says Y
and then true.
However, indentation is going to be very, very important. So
I am going to tab all of this
to make sure that everything falls under the while true statement loop.
Let me add my colon right there
and there it is. Now we're going to have the program running continuously.
And let's check to see if it will actually
check if the username we provided already exists.
So
let's run the program again.
All right. So
this time I'm, I'm gonna go with mark. Uh, password is password.
Ok. Fine,
successful. Now, you can see it's asking
for another username.
Let's try uh Nancy password. Let's go with sword fish.
Ok. It's working. Now, let us try using Mara
enter
and now it says uh oh, username already exists. Please choose a different username.
So awesome. We can see right now that the first part of our program,
the one that checks to see if
or the one that registers user and checks to see if the username already exists.
You can see right now that it's working perfectly fine
and that is awesome. So
join me in the next video where we're now going to move on to
stage two where we're going to create the function to log in the user.