1 00:00:00,009 --> 00:00:06,289 Welcome back. So now let us add the function to allow our users to log in. 2 00:00:06,769 --> 00:00:09,289 So I'm gonna say the fine and let's go 3 00:00:09,300 --> 00:00:13,149 with the login uh underscore will be the username. 4 00:00:13,680 --> 00:00:19,579 Let's add a colon. Actually, let's add a note. Ok? And we can just say login function 5 00:00:20,959 --> 00:00:22,489 and there you go. All right. 6 00:00:22,709 --> 00:00:24,659 So in order for the user to log in, 7 00:00:24,670 --> 00:00:27,579 they're going to require the username and the password. 8 00:00:27,590 --> 00:00:27,979 So 9 00:00:28,250 --> 00:00:32,299 let us start by saying username equals and then input 10 00:00:33,580 --> 00:00:39,869 in brackets codes, enter your username colon. 11 00:00:40,090 --> 00:00:40,799 OK? 12 00:00:41,470 --> 00:00:48,169 And then we can also add the password equals input brackets, 13 00:00:49,049 --> 00:00:52,069 enter your password. 14 00:00:53,060 --> 00:00:55,419 And there you go. All right. 15 00:00:55,689 --> 00:00:57,689 Now, in order 16 00:00:58,549 --> 00:01:02,900 to verify that the username and password do exist, 17 00:01:03,119 --> 00:01:06,089 we're gonna have to open up our users dot 18 00:01:06,239 --> 00:01:08,129 TXT file. So I'm gonna say with 19 00:01:08,279 --> 00:01:09,209 open 20 00:01:10,180 --> 00:01:13,949 and now in brackets, what's the name of the file? It is users dot 21 00:01:14,150 --> 00:01:14,690 TXT. 22 00:01:15,019 --> 00:01:16,839 And then what is the mode 23 00:01:17,059 --> 00:01:20,239 that we're going with? We're going with the read mode. 24 00:01:20,739 --> 00:01:23,040 OK? Because we want to read from the file 25 00:01:23,449 --> 00:01:25,910 and I'm gonna say as file 26 00:01:27,099 --> 00:01:28,110 colon. 27 00:01:28,580 --> 00:01:32,300 And now let us create a verbal called users 28 00:01:32,730 --> 00:01:36,239 that would accept the file dot Readlines function because remember 29 00:01:36,250 --> 00:01:39,589 we're gonna have to read line by line from each user 30 00:01:39,949 --> 00:01:41,930 in the users dot text file. So 31 00:01:42,069 --> 00:01:44,639 I'll say users equals to file 32 00:01:45,260 --> 00:01:48,519 dot Readlines and now 33 00:01:48,989 --> 00:01:53,019 brackets and there it is. So now we need to check if the 34 00:01:53,230 --> 00:01:56,940 credentials match any stored record. 35 00:01:57,209 --> 00:02:01,470 So what are we gonna do? We're gonna create our for loop. 36 00:02:01,669 --> 00:02:03,139 So I'm gonna say four, 37 00:02:03,339 --> 00:02:07,860 let us use the variable user in users. 38 00:02:08,350 --> 00:02:09,070 OK? 39 00:02:09,529 --> 00:02:10,460 So now 40 00:02:10,679 --> 00:02:15,300 what do we want to check? We want to check for the user name and then the password. 41 00:02:15,580 --> 00:02:19,000 So we can create two variables in here. We can go with this stored, 42 00:02:20,020 --> 00:02:21,789 OK? Stored underscore 43 00:02:22,020 --> 00:02:22,880 username 44 00:02:23,039 --> 00:02:25,740 and then comma we can also go with stored 45 00:02:25,960 --> 00:02:29,800 underscore password, right? A lot of creates two variables to represent 46 00:02:29,960 --> 00:02:32,639 the store username and then the store password. 47 00:02:33,029 --> 00:02:36,130 And now how would we check? 48 00:02:36,449 --> 00:02:38,559 We need to first of all 49 00:02:38,779 --> 00:02:42,119 check to see if the user name is correct 50 00:02:42,229 --> 00:02:45,940 and then also the password. But now take a look at this. Ok. 51 00:02:46,119 --> 00:02:51,195 The users dot CX file as it is we have Alex and then colon Alex. So 52 00:02:51,425 --> 00:02:56,714 we need to figure out a way how we can separate the user name from the password. 53 00:02:56,835 --> 00:03:00,315 And we know that it's the colon, the colon symbol that separates them. 54 00:03:00,395 --> 00:03:03,145 So we can use the split function 55 00:03:03,384 --> 00:03:05,664 to separate the user name and the password. 56 00:03:05,875 --> 00:03:09,895 But we can also use the strip function remember the strip function, 57 00:03:10,169 --> 00:03:15,279 we use that to remove any white characters or empty spaces, things like that. Ok. 58 00:03:15,289 --> 00:03:20,669 So let's go back in here. So what we're gonna do right now is I'm going to say user 59 00:03:21,199 --> 00:03:23,910 and now let us add the strip function 60 00:03:24,050 --> 00:03:26,570 to remove all the necessary space from the username. 61 00:03:26,679 --> 00:03:27,270 Ok. 62 00:03:27,509 --> 00:03:32,539 Our brackets. And now I'm going to add the split function. 63 00:03:32,720 --> 00:03:37,750 And what are we splitting the username and the password by is going to be the colon. 64 00:03:38,619 --> 00:03:43,139 And there you go. So we're gonna split the username and the password by the colon. 65 00:03:43,149 --> 00:03:44,720 So from here right now, 66 00:03:45,449 --> 00:03:46,529 we can check, 67 00:03:46,649 --> 00:03:48,149 we can say if 68 00:03:48,550 --> 00:03:49,649 the username 69 00:03:50,009 --> 00:03:51,169 is the same 70 00:03:52,039 --> 00:03:54,119 as what the store 71 00:03:55,500 --> 00:03:57,240 underscore username 72 00:03:57,419 --> 00:04:00,570 and what and password 73 00:04:01,110 --> 00:04:04,940 is equal as what is stored underscore 74 00:04:05,699 --> 00:04:07,160 password. 75 00:04:07,580 --> 00:04:08,960 What do we want to do? 76 00:04:09,240 --> 00:04:11,199 We want to print 77 00:04:11,779 --> 00:04:12,580 and say 78 00:04:13,479 --> 00:04:15,380 log in 79 00:04:15,619 --> 00:04:16,829 successful 80 00:04:17,839 --> 00:04:20,230 and there you go. 81 00:04:20,470 --> 00:04:22,709 Awesome. Awesome. Awesome. And then from here, 82 00:04:23,049 --> 00:04:27,549 we can simply return for now. Ok, later on we will add 83 00:04:28,399 --> 00:04:31,369 additional functionality. But for now, let's just return 84 00:04:31,500 --> 00:04:34,470 and go back to the beginning of the program. So we're going to exit the function 85 00:04:34,589 --> 00:04:36,529 after a successful login. 86 00:04:36,730 --> 00:04:42,459 And of course, we can also print just in case something 87 00:04:42,790 --> 00:04:44,619 did not go correctly. 88 00:04:45,000 --> 00:04:46,260 We can print 89 00:04:47,269 --> 00:04:48,570 nine brackets. 90 00:04:48,859 --> 00:04:51,209 We can simply say invalid 91 00:04:52,399 --> 00:04:53,809 username or 92 00:04:54,160 --> 00:04:55,019 password. 93 00:04:56,209 --> 00:04:56,829 And 94 00:04:56,970 --> 00:04:58,670 there you go, let me just add 95 00:04:58,920 --> 00:05:00,470 some space in here 96 00:05:01,320 --> 00:05:04,940 and there we go. So hopefully this should work let me 97 00:05:05,140 --> 00:05:08,950 integrate it into the main function. So what we want to do right now is 98 00:05:09,230 --> 00:05:10,450 where it says the 99 00:05:10,649 --> 00:05:11,880 choice number two, 100 00:05:12,019 --> 00:05:15,309 we can remove the print statements and now simply add 101 00:05:15,600 --> 00:05:17,359 the name of the function which is login under 102 00:05:17,579 --> 00:05:18,179 user 103 00:05:18,709 --> 00:05:22,059 and now brackets and there it is OK, 104 00:05:22,899 --> 00:05:25,519 fingers crossed. Let us run the program 105 00:05:26,869 --> 00:05:28,739 and let us see. 106 00:05:28,750 --> 00:05:31,230 I'm gonna go straight to log in because 107 00:05:31,239 --> 00:05:34,600 we've already created the Alex username and password. 108 00:05:34,609 --> 00:05:34,880 So, 109 00:05:35,700 --> 00:05:41,880 user name is Alex, password is Alex. And there you go. Awesome. Login successful. 110 00:05:42,329 --> 00:05:45,160 Let us try to see if we'll accept 111 00:05:45,290 --> 00:05:48,519 a different username that doesn't exist. So 112 00:05:48,859 --> 00:05:52,070 I'm gonna go with login and let's go with Philip 113 00:05:52,959 --> 00:05:56,179 enter password, sword fish. 114 00:05:56,760 --> 00:06:01,410 And there we go, it says invalid username or password. So it is working. Awesome. 115 00:06:01,420 --> 00:06:02,290 Awesome. Awesome. 116 00:06:02,600 --> 00:06:05,489 So we've successfully added the registration function. 117 00:06:05,500 --> 00:06:09,059 We've successfully added the login function as well. 118 00:06:09,200 --> 00:06:12,440 So now join me in the very next video where 119 00:06:12,480 --> 00:06:17,380 we're going to advance the program by introducing the password checker 120 00:06:17,559 --> 00:06:23,019 that will ensure that the password created meets a certain strength criteria. 121 00:06:23,029 --> 00:06:23,739 I'll see you then.