 Hello, this is Christian. In this video, we're going to write a Python program for this credit card number check application. And so the idea is to check if a credit card number is a valid number or not. And for this exercise, we're just going to focus on the eight digits, okay? Of course, it should also work for 16 digits. But in this exercise, we're just going to focus on eight digits. So the last digit of the credit card is the check digit, which is used to determine the validity of a credit card. And the last digit here is referred to the last digit down here, when we do this sum, okay? So before we get here, again, just say that we look at these eight digits here. We're going to do the following, okay? You're going to start from the right most. So you want to follow these instructions. The reason why you start from the right most is because that some credit cards are not always 16 digits, right? Some have 15 digits, like American Express, for example. So you want to write a program that would work for every credit card. Okay, if you start from the left to the right, then you may get incorrect answer. All right, so we're going to look at these numbers and start from the right most digit, like in this case, it would be five, we start from five, and then we count backwards, we skip one, and then we'll go five, seven, and then eight, and then three, okay? So you get those numbers out, and you add them up, you get a sum of 23 in this example, and you save that to a sum. So you can call it sum one, or two, or whatever it is, okay? So you start the number. And then the next thing is you're going to grab the other digits, which you did not use in the previous step. So in this case, it would be nine, nine, five, and four, okay? So again, from the right to the left, so each of those digits, you're going to double it, meaning you're just going to just basically times two, add it so twice, okay? When you do that, you're going to end up with either a single digit or a two digit number like this, right? So if you take the nine, for example, if you double it, you get 18. And then what it wants you to do is you're going to take these two numbers and split them up, if you will, or and then get them or separate them, and then add them to each other to get a total of nine, right? So you add them up, you get nine. Here again, you add them up, you keep adding each digit, single digit together, and you're going to end up having something like this, and you have an equal, you have a sum of total of 27 in this example, okay? So you have two sums. So the first sum is 23, the second is 27, and then you add them up and you get a total here, okay? So this is what the magic happens. When you get this total, all you have to do is check this last digit, okay? You may have two digits here, you may have three digits depending on the total number of card numbers you have here. So you're going to check the last digit. If that digit is zero, then that is a valid number, okay? If it's anything other than zero, then that is not a valid number. So that's all you have to do for this assignment. Of course, I think I may ask you to determine which number is, you need to plug in here to make it valid, right? If you have an invalid number, how do you know which number should plug in, should you plug in to make it valid, okay? So we're not going to do that part. All you have to do is just determine if this is a valid number or not. And here I also listed a few numbers, examples that you can use in tests and they should also be valid as far as I'm concerned, okay? So let's see how this is done. I'm going to do this using two approaches. The first approach, I'll use the conversion between numbers and strings and vice versa. And then the second approach, I'll do it in a separate video to show you how to do it using just the mathematical operators, the flow division and the modulus. All right, so I'm going to copy these numbers here to force the test. And then I'm going to go right into the spider IDE. And so I'm going to go and create a, let's create a folder in here. I'll call this one here, the credit cards. All right. And, well, I should, I shouldn't call that, but anyway, I'll go, another one, cc.py, okay? That is the application here. And then I'm going to put the doc string here. This is called the doc string, by the way. Every time you see something like this in a module, this is called a module. These are called doc strings. It's the first string of text you put here. And this basically just describe what this app is all about. But anyway, that's on a different day. So now these are our sample numbers. Okay, so what are we going to do? Well, we want to come in and put on to a single digit. First of all, I want to just go over what it means when you're using strings. So for those of you who already know this, you can skip the video, but I'm going to explain how you get those positions in the string, right? So remember a string is really a container, a container of character. So think of these as a single character in a string. Okay, so each of these containers has a position. So the first position is always going to be zero, right? It's a zero base language, zero, one, two, three, four, five, six, seven, and so on. Okay, so the last number is this seventh position. Okay, so now I'm going to just create a variable called card. And that is going to store this number. Okay, so of course, we're going to read this from the console. But for now, just to make it work, we're going to test it first just by using this plain string here. All right, so now then we're going to convert this to a string. Okay, and we use a string method. So I want to do a temp. I want to preserve my credit card number. I don't want to change it. So I'll keep it as original and I'll convert this to a string. So my card is now stored attempt as a string. And then I need to store the sum for the first group and the sum for the second. So I've just got to call sum one is zero and sum two is zero. Okay, and then now we are going to create a while loop. So the while something is true, right, and we process something. So what am I doing here? Okay, so again, the exercise asks you to count numbers from the rightmost and going backwards, right? So therefore I'm going to need an index. The index is going to be pointing to the end of this character. Well, to get that character is the length of this string, right? So this string here is going to be eight, for example. So don't put eight here. If you just do that, it only works for that case, right? You want to write a program that will work for any case. And so to do that, you want to get the length of the temp. Okay, so you want to start from the last position. So the length gives you eight, but the index here is really seven, right? If you do eight, you can get error. It would say, you know, out of boundary, out of bound index problem. Okay, so that means I have to subtract one to start at the last position. You can either do here or you can do inside your code somewhere. So since I want to start from the very far right, I want to do a minus one right here. So I don't have to worry about that afterwards. Okay, so now you can say something going from right to left, I'm going from top to bottom. I'm going to say if while index is greater than or equal to zero, okay. Well, yeah, it goes zero because we include zero as well. We're going to come all the way through depends. If that's true, then we go into process the index in here. So the first part is going to be very simple. For this one, I'm going to do the sum one. So this is for the sum one. I put here, calculate sum one. Okay, so again, this is just a doc string, kind of a different way to document your code. And I'll explain this maybe later if you were interested what it means, but it's for a doc string. But I'm going to calculate the sum one. Okay, so in here, I want to say the sum, right, this is a very simple one. So the sum one is going to be plus equal to what am I doing? I'm going to basically add all these numbers. That's all I'm doing. Add this to the sum, add this, and then add this and that. So I'm going to say add the temp, which is the temp character string here, of the index position. I add that to the sum. But remember, sum is a number, and these are these are now characters. So you want to convert that to an integer. Okay. And so I convert that to integer, add it to the sum, my accumulator, and then finally just make sure you increment you decrement your index. So index is minus equals one, right? Actually, no, two, right? We're moving two steps. Remember, we're skipping one. So I'm starting here, if I minus two, I'm going to jump right to the next number, and then jump to the next two, and so on until we add, this is true, right? And so I'm going to go to the next number, to the left, and add them up. And eventually, I'm going to get a sum of one. Okay, so this part is pretty much that's it. It's very simple. This is the easier part. Now, the second part is now we're going to double each of these numbers starting from the second, from the left, and then you go back, you're skipping one, you double those, add the digits together, if there are two digits, you keep going, and then you're going to total all together and add it to the sum two. Okay, so I'll do the same thing. This timer index is going to be also from lend temp. This time, I'm going to go from, I'm not going to go from here, I'm going to go from the second digits. So let me add to minus two, right? Starting from the minus two position. So here again, so while index is greater or equal to zero, and then we'll do something similar, I'll put here, calculate sum two. Okay, so now what do you do? I'll just make some lines and you can see. All right, so we need to grab the number, which is in this case, the index of the temp of index. So I need to get the temp of index. And I need to multiply that by two, right? So before I do multiplication, I have to convert it to an integer first. You can, you can multiply, otherwise you're going to get a different result. Then multiply by two. Okay, so now, if it's a nine, then that is now 18. Okay, so I'm going to store that to another variable called n. So now n is 18 in this case, if the first number is nine. And the next thing is I'm going to add the one and the nine, right? So in this case, I get 18. Because you know, right, nine plus nine is 18. So I'm going to add the one and the nine together. But there are some cases like the number four, right? Number four, if you add them up, you only get a single digit. Okay, so you kind of have to do some checking here. And you know that to get a two digit, you know, any number above five will always give you a two digit, but four or less than a single digit. So what you can do is you can grab, you can convert this to a string, right? Convert this to a string. And if it's a two digit number, and then grab each number, each position like index zero, index of one, and you add them up, convert to integer, add them up, and then you add it to the sum. Okay, if it's a single digit, then you just add the digit to the sum right away. Right. So here I would say something like if n is greater or equal to 10, right? Because again, the only way we can get two digits is if it's, if it's a five, right, five plus five is 10. If it's greater than 10, then I mean it's a two digit number. And so if that's the case, you can go ahead and proceed as follow. Of course, there are many ways to do it. You can grab, you know, if, if the temp of index, right, I can also do this, if I say a template index is, you know, greater or equal to five, right? Same thing. Then I'm going to get a double digit. Okay, so since I already calculated my n, I'll just use n here. If that's the case, then I'm going to convert in to a string. So I basically can reconvert to a string now. And since it's a string, I can grab the number, the, and the zero index position, and the one index position, and add it to the sum. So my sum two plus equal the first position, which is n of zero, right? And then convert to an integer, because it's a string, right? Plus the second digit, so integer n of one. And add us to numbers to the sum. So if one plus eight, we get nine, add in here, and then we're done. If it's not a two digit number, then we go to the else part. And we just basically sum two, we just plus the n. That's it. Because here, we did not do conversion, right? Outside here is a conversion. It's an integer. But in here, we convert it. So this one here has nothing to do with this one here. So it's already integer. We don't have to do, you know, this process, just add it to the sum. And we're done. And then finally, when we're out of that, if else block, we just make sure to index minus two, okay? Update or index. And I believe if that's correct, that should give you the total for the second part. And so now, the last part here is you're going to check for validity, right? So you can check here, check for the check digit. I'm not sure, I forgot what he says, but okay. Or you can say, you can say, maybe verify, check digit. Okay. So now, you have to do is just say total equals sum one plus sum two. And then now you need to check those two numbers and see if the last digit is equal to zero. And yeah, again, a couple ways to do this. But since I'm using strings and numbers, I'm going to use that same method, okay? So I'm going to say something like a valid value equals I'm going to convert the total to a string, right? Maybe I do this. I can do all at once here. So I can do like a string, the whole thing, right? The whole thing is now it's a string. And then I can check the last value, total of the right most, in this case would be one, right? If it's a, since I only deal with two digits, you can do that one. If it's a three digit, it won't work, right? So the safer way to do is you can do, again, grab the length of the total minus one. I always guarantees you that last position, okay? It doesn't matter what size is this is, this is. And so I grab my valid value, and then I can check my valid value. So say if valid value is a zero. It's a, this case, I'm, I have a character, you can check against a character zero, if you want to, since it's a character. If you want to check as a number, just again, make sure you convert this to integer like this again. So you can see it says a lot of a convergence back and forth. If you do that, then it's a, it's a number. So change that to a number, okay? Even showing you different ways to do it. And then now if it's zero, in this case, you can say print, just put here valid. Else, I'll print. Just put in valid here. Of course, you can put more meaningful messages, but this would do it for now. All right, so let's see if this works. I have not checked it. I didn't run it before. So let's see if this works, okay? So let's run it and we'll see what it says in the console. And here it says it's valid, right? It's valid just like the example it says. We can also check up here manually. We see that the total, if you add them up, someone is 23. Some two is 27, just like the example in the document. If you add them up, you get a total of 50, and 50 is indeed a valid number. And if you go and switch this number to something else, say change it to a two, you save it and run it. It's invalid because the sum is indeed 47 and it's not a zero. All right, so that works fine. Let's go back and grab the other numbers and we can check them. So these right here. So let me copy these and go back to the program and let's check those. Of course, I could have, you know, use a better input, but since I already have it here, I just put it here. Okay, so I put it here. And actually, you know, let's do that. So I want to do a, okay, let me do this. My template is already string, so I'm going to come using as an input. And it's just an enter number. Okay, we'll read those into the number. Okay, let's save it and run. And so here I'm going to copy this group. Press right here. And so that's valid. That's good to go. The next one. And we'll do it again. That's valid. As you can see, so these are all valid numbers. And we do one more here. I think we should be good to go. Okay. So as you can see, that works perfectly fine. So in the next video, I'm going to show you how to do a using a for loop. Most of these would be the same, but the full will be structure a little bit different. So I'll see you in the next video.