1 00:00:00,409 --> 00:00:01,309 Welcome back. 2 00:00:01,320 --> 00:00:06,190 Now, let's move on to the second stage where we're gonna create our function to 3 00:00:06,670 --> 00:00:09,270 log in an existing user. 4 00:00:09,579 --> 00:00:14,550 What I'm gonna do is I'm gonna push the function here all the way down to the very end 5 00:00:14,760 --> 00:00:16,629 and outside. Let's 6 00:00:16,860 --> 00:00:22,350 add a new notes that says function to log in the user. 7 00:00:22,850 --> 00:00:23,379 So 8 00:00:23,530 --> 00:00:27,170 what do we need? Let us first of all, create the name of the function that will be de 9 00:00:27,280 --> 00:00:30,389 let's say login underscore user 10 00:00:31,450 --> 00:00:33,380 and let's add a colon. 11 00:00:33,590 --> 00:00:38,770 So what are we going to need? We're going to need both the username and the password. 12 00:00:38,779 --> 00:00:39,200 So 13 00:00:39,340 --> 00:00:40,950 let us say username 14 00:00:41,689 --> 00:00:45,450 equals and now input, we're going to prompt the user to provide 15 00:00:45,709 --> 00:00:47,389 their username. So 16 00:00:47,700 --> 00:00:50,860 enter your username. 17 00:00:51,240 --> 00:00:53,259 Ok. Call on, let's add 18 00:00:53,659 --> 00:00:55,060 some space right there. 19 00:00:56,580 --> 00:00:57,319 Ok. 20 00:00:57,549 --> 00:01:01,029 Next will be the password, right? So I'm gonna say password 21 00:01:01,650 --> 00:01:02,900 equals and then 22 00:01:03,080 --> 00:01:06,540 enter your password pretty straightforward. 23 00:01:06,860 --> 00:01:12,959 We're going to have to now check to see if the username actually matches the password 24 00:01:13,180 --> 00:01:14,260 that we had in the 25 00:01:14,510 --> 00:01:16,669 registration section. So 26 00:01:16,980 --> 00:01:19,660 I'm going to add my note real quick 27 00:01:20,680 --> 00:01:29,040 check to see if username and password match. Ok. So how do we do this, the good old if 28 00:01:29,459 --> 00:01:30,900 and now username 29 00:01:31,169 --> 00:01:32,569 and user 30 00:01:32,919 --> 00:01:33,230 on the 31 00:01:33,349 --> 00:01:33,540 core. 32 00:01:34,150 --> 00:01:36,029 Oops, sorry about that 33 00:01:37,819 --> 00:01:38,190 in us. 34 00:01:38,290 --> 00:01:41,940 So if username in user credentials and 35 00:01:42,199 --> 00:01:45,080 because both username and password have to be checked 36 00:01:45,620 --> 00:01:46,860 and user 37 00:01:47,510 --> 00:01:49,449 underscore credentials 38 00:01:49,849 --> 00:01:52,129 and now the brackets 39 00:01:53,169 --> 00:01:54,230 username 40 00:01:55,599 --> 00:01:59,129 equals to the password. 41 00:02:00,089 --> 00:02:00,779 All right. 42 00:02:01,010 --> 00:02:02,180 So this is what we did here. 43 00:02:02,400 --> 00:02:05,839 We're checking to make sure that if the username 44 00:02:05,879 --> 00:02:08,809 that's already been stored in the dictionary user credentials 45 00:02:09,100 --> 00:02:10,429 and now 46 00:02:10,649 --> 00:02:14,910 the key value pair for the username and password, if they actually match, 47 00:02:15,229 --> 00:02:17,389 then we can print. 48 00:02:18,259 --> 00:02:19,800 All right, I'm gonna say print, 49 00:02:21,779 --> 00:02:25,139 let's just say, welcome back. 50 00:02:25,490 --> 00:02:25,770 All 51 00:02:26,619 --> 00:02:27,399 very, very, 52 00:02:28,270 --> 00:02:29,460 very simple. 53 00:02:30,089 --> 00:02:31,740 If it's not a match, 54 00:02:32,210 --> 00:02:33,789 we can print 55 00:02:35,440 --> 00:02:36,679 now, we can say 56 00:02:37,929 --> 00:02:42,070 uh invalid username and password, please try 57 00:02:42,919 --> 00:02:43,520 again. 58 00:02:45,029 --> 00:02:45,649 All right. 59 00:02:46,520 --> 00:02:48,559 And there it is pretty straightforward. 60 00:02:48,990 --> 00:02:54,539 So what I'll do right now is I am going to call both of the functions, register user 61 00:02:55,380 --> 00:02:56,759 and then also 62 00:02:57,270 --> 00:03:00,880 uh log in underscore user. 63 00:03:02,600 --> 00:03:03,869 However, 64 00:03:04,460 --> 00:03:08,110 we need to pay very close attention to the fact 65 00:03:08,300 --> 00:03:15,470 that the loop currently resides inside of the register function. 66 00:03:15,869 --> 00:03:18,210 It is not outside. 67 00:03:18,729 --> 00:03:25,029 So we need to run our loop independently of the register function and 68 00:03:25,240 --> 00:03:30,669 the login function. So what I'll do is I am going to cut out 69 00:03:31,210 --> 00:03:32,460 the while true, 70 00:03:33,130 --> 00:03:34,179 cut it out. 71 00:03:34,380 --> 00:03:35,039 OK? 72 00:03:35,270 --> 00:03:36,339 And then 73 00:03:36,529 --> 00:03:38,509 I am going to paste it 74 00:03:38,679 --> 00:03:40,020 outside 75 00:03:40,160 --> 00:03:41,619 on its own 76 00:03:41,759 --> 00:03:46,979 while true. However, everything else 77 00:03:47,149 --> 00:03:48,660 must fall 78 00:03:48,889 --> 00:03:51,179 under it. So I'm going to table it 79 00:03:53,139 --> 00:03:54,149 everything on there. 80 00:03:55,899 --> 00:03:57,690 Uh Let me do this again 81 00:03:58,949 --> 00:04:01,880 and there you go. So there, so there it is. 82 00:04:02,130 --> 00:04:04,770 So you can see right now that the user credentials 83 00:04:04,910 --> 00:04:07,740 and then the wire loop are outside the independent. 84 00:04:07,919 --> 00:04:10,850 Now the wild should the entire loop begins with 85 00:04:10,979 --> 00:04:12,410 registering the user 86 00:04:12,630 --> 00:04:16,140 and then logging in the user checking to see if there are matches, things like that. 87 00:04:16,149 --> 00:04:20,250 So this should work. I'm going to go ahead right now to run the program 88 00:04:20,998 --> 00:04:22,979 and let us see. So 89 00:04:23,338 --> 00:04:25,239 I'm going to provide the first username. 90 00:04:25,248 --> 00:04:28,498 Let's go with Mark and then password will be admin 91 00:04:28,908 --> 00:04:28,928 for 92 00:04:29,269 --> 00:04:31,848 successful. Now let us try to log in 93 00:04:32,118 --> 00:04:35,098 as mark. So I'm gonna say mark and then admin. 94 00:04:35,750 --> 00:04:37,730 So mark and then admin 95 00:04:38,429 --> 00:04:41,149 and there you go. Welcome back. It works successfully. 96 00:04:41,160 --> 00:04:44,130 Now it's gone back to the registration phase. 97 00:04:44,390 --> 00:04:48,529 If I try to now create another account with Mark, 98 00:04:48,660 --> 00:04:53,809 it's going to say username already exists. Please choose a different username. 99 00:04:54,049 --> 00:04:58,200 Let's go with Nancy. Ok, password for Nancy. 100 00:04:58,359 --> 00:05:00,079 Let's go with password. 101 00:05:00,739 --> 00:05:05,470 Ok. And now you can see it says sorry, any valid user name pass. Please try again 102 00:05:05,700 --> 00:05:08,559 the reason why this happens because 103 00:05:08,899 --> 00:05:11,850 the way the program is written out right now, 104 00:05:11,940 --> 00:05:17,070 it simply goes step by step by step by step regardless of whether or not 105 00:05:17,230 --> 00:05:19,440 the previous step was successful. 106 00:05:19,570 --> 00:05:23,480 So over here, you can see it set off with the registration phase 107 00:05:23,649 --> 00:05:27,630 that worked, it then moved on to the login phase that worked. 108 00:05:27,739 --> 00:05:30,559 It now came over here for the login phase again. 109 00:05:30,820 --> 00:05:30,829 Oh, 110 00:05:30,959 --> 00:05:36,380 sorry for the registration phase again. It saw that mac already exists as a user. 111 00:05:36,390 --> 00:05:39,109 So it said please choose a different username. However, 112 00:05:39,529 --> 00:05:40,769 it skipped 113 00:05:40,899 --> 00:05:44,350 asking the user to create another username over here. 114 00:05:44,359 --> 00:05:49,350 It went straight to the login phase where it checks to see if Nancy existed. 115 00:05:49,500 --> 00:05:49,649 NSA 116 00:05:49,850 --> 00:05:52,109 did not exist. That's why it said invalid 117 00:05:52,260 --> 00:05:53,420 name. A Puzo. Please try again. 118 00:05:53,429 --> 00:05:57,440 So right now it has gone back to the registration phase. So 119 00:05:57,799 --> 00:06:01,049 if I say Nancy again, 120 00:06:01,170 --> 00:06:05,309 now you can see it's asking Nancy for the password because it's in 121 00:06:05,630 --> 00:06:06,640 registration phase. 122 00:06:06,880 --> 00:06:08,459 Now, if I type in admin, 123 00:06:08,690 --> 00:06:09,380 it says we 124 00:06:09,549 --> 00:06:14,209 successful. Now let us try logging in because now I was going to the login phase. 125 00:06:14,220 --> 00:06:16,260 I say Nancy, 126 00:06:16,709 --> 00:06:18,140 Nancy's password is admin, 127 00:06:18,149 --> 00:06:20,970 but I'm gonna type in something different enter and 128 00:06:20,980 --> 00:06:22,619 then it says invalid user name or password. 129 00:06:22,630 --> 00:06:24,660 Please try again. 130 00:06:25,140 --> 00:06:26,089 So 131 00:06:26,500 --> 00:06:31,720 we can see right now that the program works kind of like 80% 132 00:06:32,079 --> 00:06:35,720 the registration phase, the login phase work. However, 133 00:06:36,670 --> 00:06:39,119 if we were to ideally 134 00:06:39,269 --> 00:06:42,690 when we're trying to create a new user account, 135 00:06:42,940 --> 00:06:46,579 and if we are trying to use a username that already exists, 136 00:06:46,679 --> 00:06:53,179 the program should not move on to the second stage, which is to log in the user until 137 00:06:53,369 --> 00:06:59,149 the user has actually provided a brand new user name for the registration. 138 00:06:59,739 --> 00:07:01,010 We're gonna fix that 139 00:07:01,470 --> 00:07:04,489 in the third and final part of this assignment.