Welcome back.
Now, let's move on to the second stage where we're gonna create our function to
log in an existing user.
What I'm gonna do is I'm gonna push the function here all the way down to the very end
and outside. Let's
add a new notes that says function to log in the user.
So
what do we need? Let us first of all, create the name of the function that will be de
let's say login underscore user
and let's add a colon.
So what are we going to need? We're going to need both the username and the password.
So
let us say username
equals and now input, we're going to prompt the user to provide
their username. So
enter your username.
Ok. Call on, let's add
some space right there.
Ok.
Next will be the password, right? So I'm gonna say password
equals and then
enter your password pretty straightforward.
We're going to have to now check to see if the username actually matches the password
that we had in the
registration section. So
I'm going to add my note real quick
check to see if username and password match. Ok. So how do we do this, the good old if
and now username
and user
on the
core.
Oops, sorry about that
in us.
So if username in user credentials and
because both username and password have to be checked
and user
underscore credentials
and now the brackets
username
equals to the password.
All right.
So this is what we did here.
We're checking to make sure that if the username
that's already been stored in the dictionary user credentials
and now
the key value pair for the username and password, if they actually match,
then we can print.
All right, I'm gonna say print,
let's just say, welcome back.
All
very, very,
very simple.
If it's not a match,
we can print
now, we can say
uh invalid username and password, please try
again.
All right.
And there it is pretty straightforward.
So what I'll do right now is I am going to call both of the functions, register user
and then also
uh log in underscore user.
However,
we need to pay very close attention to the fact
that the loop currently resides inside of the register function.
It is not outside.
So we need to run our loop independently of the register function and
the login function. So what I'll do is I am going to cut out
the while true,
cut it out.
OK?
And then
I am going to paste it
outside
on its own
while true. However, everything else
must fall
under it. So I'm going to table it
everything on there.
Uh Let me do this again
and there you go. So there, so there it is.
So you can see right now that the user credentials
and then the wire loop are outside the independent.
Now the wild should the entire loop begins with
registering the user
and then logging in the user checking to see if there are matches, things like that.
So this should work. I'm going to go ahead right now to run the program
and let us see. So
I'm going to provide the first username.
Let's go with Mark and then password will be admin
for
successful. Now let us try to log in
as mark. So I'm gonna say mark and then admin.
So mark and then admin
and there you go. Welcome back. It works successfully.
Now it's gone back to the registration phase.
If I try to now create another account with Mark,
it's going to say username already exists. Please choose a different username.
Let's go with Nancy. Ok, password for Nancy.
Let's go with password.
Ok. And now you can see it says sorry, any valid user name pass. Please try again
the reason why this happens because
the way the program is written out right now,
it simply goes step by step by step by step regardless of whether or not
the previous step was successful.
So over here, you can see it set off with the registration phase
that worked, it then moved on to the login phase that worked.
It now came over here for the login phase again.
Oh,
sorry for the registration phase again. It saw that mac already exists as a user.
So it said please choose a different username. However,
it skipped
asking the user to create another username over here.
It went straight to the login phase where it checks to see if Nancy existed.
NSA
did not exist. That's why it said invalid
name. A Puzo. Please try again.
So right now it has gone back to the registration phase. So
if I say Nancy again,
now you can see it's asking Nancy for the password because it's in
registration phase.
Now, if I type in admin,
it says we
successful. Now let us try logging in because now I was going to the login phase.
I say Nancy,
Nancy's password is admin,
but I'm gonna type in something different enter and
then it says invalid user name or password.
Please try again.
So
we can see right now that the program works kind of like 80%
the registration phase, the login phase work. However,
if we were to ideally
when we're trying to create a new user account,
and if we are trying to use a username that already exists,
the program should not move on to the second stage, which is to log in the user until
the user has actually provided a brand new user name for the registration.
We're gonna fix that
in the third and final part of this assignment.