 Hello, this is Christian. In this video, we're going to write a tiny Python program to print the barcode of the five-digit zip code to the console. So here is the instructions. So here is an example of the barcode you will see on the US mailing system. And this barcode, if you break it down to your own digits, you'll see it is represented down here. All right, so the first barcode, or this line here called, this is the full bar, the first one, and the last one here are known as the frame bars. So when you print it out, you always print the frame bars from the beginning and the end. Okay, so in between is a set of five bars, either a full bar, half bar, or a combination of thereof that will encode a digit. So here's the first digit, second digit, third, fourth, and fifth. So this doesn't mean represent a digit one, it just means the first digit. These digits are exactly what this digit will be encoded with, right? So nine represents these five bars here, and then five represents this, and so on. So the sixth digit is what's called a check digit. This check digit is calculated by adding all the numbers of these five digits. So if you add nine plus five plus zero plus one plus four, you're going to get 19. And the idea to get this check digit is basically you check at this number. If it's not a multiple of 10, right, not a multiple of 10, then what you do is you subtract this number from the next largest multiple of 10. So in this case, as you can see, it's between 10 and 20. So then what you do is you subtract 20 into 19, or you subtract 19 from 20, rather, to get a number of one. And this difference or this number is what's called the check digit. And there are a couple ways to do this as well. This is an easier way, and there's also a long way to do that. But we'll do the shortcuts, you can see how easy it can get this number here. Okay, just remember it's a multiple of 10, right? All right, and then down here, you are going to see a chart. Now, this chart is already, you know, calculated, created, so you don't have to do anything else. But how to get this chart is explained down here. So you're not going to do any calculation using these weights at all, it's already been done for you. So if you look at these digits in the first column, these are the actual numbers, right, from a one to nine and then including zero. The reason why they put zero on the bottom here is because if you look at these numbers like zero, zero, zero, you start with zero, zero, zero, zero, and then the largest is down here. That's why it's placed down here. But really, so for example, at the zip code number 95014. So if you look at number nine, there's nine, who has this five bars right here. So the number one as stated here represents a full bar, then zero is a half bar. So you get full bar, half bar, full bar, half bar, half bar. And if you look at this, you see down here. So here is the, the first bar is the frame bar. And the number nine represents here, right? So it's a full bar, half bar, full bar, half bar, half bar. So that's nine. And then five is the next one. You see that half full, half full, half. And that's half full, half full. And so on. You go all the way to the ninth, fourth digit. The last digit is the check digit. And then you put your frame bar at the end. Okay. And then I'm going to ask you to use these five functions, I mean four functions rather. Of course, you can add more if you need to, but at least have these functions in here. The main function, the get digit is to read the five digit zip code. And then the print digit that will receive a parameter called D for each single digit. And you get to print that out using the barcode and the barcode representation. And then the function print barcode will you pass entire zip code to it. And it will print that entire zip code. Okay. Okay, so let's see how this is done. Now I'm going to already copy this, you know, functions over to a file. And also, I created the representation barcode representation for each of these digits already. So let's go to the IDE and see what that looks like. Here in the IDE, I created a file called postal barcode.py. And I copied over the functions that we're going to create or implement. And also put it in the global constants. Some of these numbers in their encoded representation, right? So I put 1 1 0 0 0. The one is the full bar, zero is the half bar, right? I'm doing this way versus something like this. You know, full bar, full bar, half bar, half bar, half bar, right? You can do this if you want. If you do this way, then you basically save yourself a step, you don't have to convert each of these numbers. But I guess it's a little bit less fun. And also, if you make changes in the future, let's say that, okay, we have a different system now and numbers are now changed, you can always still come back and change this to a full bar, half bar, as someplace else. Or you can use this binary number, this number here, it looks like binary numbers. And I'll prefer this way because now if you look at this, this is more portable, right? If you happen to reuse this file again, then you can just basically export this out to another module. And these numbers because they're digits, they're, you know, more flexible and more portable than these, you know, it's very too specific bar code. So I'll use the numbers in here, I'll do another loop to loop through each of this digit and we'll convert by using either the full bar, excuse me, full bar or half bar, right? Okay, so let's go down here and start a program. So the first thing is down the very bottom is just to make sure we have a notice a program starts here, I just put that note here so I know what happens. And just make sure you always invoke the main method down there. Okay. All right, so what happens when you run the program? So you go to the main function. The first thing to do is you need to get the digit. So this function will do exactly that. So the first step is we're going to set a variable called zip code. And this can be assigned to that get digit function. And this function does not receive any parameters. So we're just going to leave that. And once we get the digit, then after that was the next step, we're going to pass that digit to code to this print barcode function. So here then, we'll just say print barcode, pass that to it. And if you look at this main function, that's all there is to write really short. Of course, you can do the calculation first. And you pass that over. But you know why do here your main function supposed to be very short and sweet and very kind of generic. Okay, so let's look at this function first to get digit up here. And this is as you as to put a note here is to basically read in the five digit. So you can say, you can just return right away. I'm not validating anything at all. If you are validating digits, make sure they are digits, then of course, you're going to have more statements. But I'm going to do just a simple statement was assume that the digits are all numbers. Okay. So I'm going to return the input. I'll say enter a five digit zip code. We enter the five digits of code, and we'll pass it back return it as a string to the zip code. So now this is a string. And then we pass a string of zip code to this function. And then now we are now here. So this is pretty much done, right? Very short like that. And I mean, let me do this, we can have some more spaces. So now what do we do in the print barcode function? The idea is to print it out, right? So the first thing we need to do is we need to print the the full bar. This is the frame bars at the beginning. And we want to remove the the line break. So at the end of parameter and set that to empty string. This is the front bar frame bar. Also need one for the end. So you can copy this, we can press the alt control and down arrow to duplicate this line. And this is the end bar and frame bar. So that's beginning the end. And between is where we actually print the five digit plus the check digit. So the first thing we need to do is we need to loop through this barcode. Every digit, we pass that to this function print digit to print it out to print the encoded barcode, right? So here, we'll do a very simple loop. I use the full loop for every D or digit in the barcode zip code, sorry, grab every digit, and then we're going to pass that to the print digit function, pass D to it. Okay, so here, I put a note here. We can say print each digit. And then once we get all those five digit printed, the next thing is here, we need to print the check digit. Well, in order to print it, you have to calculate it first, right? So instead of calculating here, you want to send it off to another function. So I'll use another function to do the calculation. Then that function will return that check digit back to another variable. I will also just call it check digit is equal to the, I guess we'll call it a function called calculate check digit. I passed to that the actual zip code. I need the entire zip code to be calculated. And once I get the check digit back, then I can go ahead and print it out. So print again, print digit, pass this check digit to the print digit function because it's indeed a string character, right? So this is the print barcode. Now we need to implement this each digit barcode, right? We need to print that out. Again, the function is to print, so that's exactly what we're going to do in writing here. So we got the digit coming in. This is a single digit. What do we need to do? We need to get the representation of each of these numbers. If it's a zero, then we're going to return this or print this out. If you already have it in this format, then you can just print it. Since I don't, I need to loop through each digit to make sure that it's properly encoded to using the four bar or half bar to get something like this before I print it out, okay? So I'm going to do something like this and the print digit. So you have to do something like if the D is indeed a zero, if it's like that, then you need to loop through the zero constant, right? So you can say for every digit in the zero. And then I need to check if the digit is indeed a, if it's a zero, right? Then I need to print the half bar else. It must be a one, print the full bar, okay? You print that out. And that's it, right? Half bar, half bar, full bar, and you print that out. But when you print it out, you need also to make sure you have to encode the end is equal to blank and then something here, okay? So this is only for if it's a zero or a one. I mean, all of this is for one digit. So that means I will end up having something like this. Oops, it looks like LIF. It's a one. Then you do the whole thing again and again. You see that it's very cumbersome. It does take a lot of code. When you see repetition, your code like this, you break it into a function, right? So you don't have to do this because you can pass in the digit to another function. And that function can do the conversion for you, do the conversion for you. And then you return it back and then just print that out, right? So you don't have to do this for every zero, one or two. So I would say that I might need this. I just need one conversion. And then so I don't need this part here. But I'm going to get the digit coming in. Instead of doing this way, zero, one, two, three, you can get another function to return that digit. So you would do something like maybe I'll call it a code digit. Digits mean all the five digits in this up here. I'll return the correct one. So I will assign that to a function. Maybe I'll say get code digits. I passed this code digit. The actual digit is received. Okay. And then you return back either either zero through nine, one of those. And then you can just check it right here once. So you can say if you don't need this anymore. So I need this one here. I still need that over here. So I can check each digit in the coded digits here. If it's a zero, then you print this half bar. If it's a one, you print the full bar. You can do that for one at a time if you want. Or you can build the bar code first. So you don't have to print each one at a time. You build a bar code. So you could do something like this. Say bar code equals to empty string first. And if it's a zero, then you want to say bar code plus equal the half bar, right? Else, the bar code is equal to the full bar. So I'll build the bar code here. Once I'm done, then I'll print it only once down here. So print the bar code. And again, you put the end character with the empty string. All right. So that's probably the way I want to do it. Okay. And then now we need to calculate this function, get the digits, so we can return it back here. So I will probably write another function up here. I put here called, what do we want to call this? I guess we're going to get the bar code representation for a digit. Okay. We're going to get one of these up here. I'm trying to say. So here if we do it, we define a function called get code digits. We'll receive the actual digit. Okay. The digit here, which is D. You can call it D2 if you wanted to matter. And then we'll do the if and else in here or the if block. So say if the digit is a zero, then we're going to just return the zero back. Okay. And if you want to build, you know, have a variable like this, you know, D is equal to something. And then if it's zero, you can say D is equal to zero like this first. And in the end, in the end, you'll return, you know, D, right? That's fine. You have to do that for all those 10 numbers. Or you just return right away like I just did. And then you have to do the last one here, right? So and also you don't need this extra variable. That means I'm going to do this for all those nine digits. So again, control Alt, copy this like 10, nine times. Let's see, I might have too many, but let's see. One, two, three, four, five, six, seven, eight. Now, yeah, this is too many. All right. So this would be nine, eight. Okay. So I return my statements. And all my statements, we turn the actual numbers representation to this variable called digits, and then I loop through every digit and do the conversion here, build my barcode, and then print it out. So again, print digit, you print digit right here, although you have to do a little bit of calculation here. Okay, so that part is done. Now down here, when we do the check digit, so we have to write another function to check the digit. So let me copy this. So I don't have to type it. I make sure I type it correctly. I put right above here. And definition of function called that. I put here, I'll just say calculate check digit. Okay, so we need to loop through each digit and you add them up. That's all. So you have a sum initially was set to zero. And for each digit in the zip code, we're going to add that to the sum. And so digit, but you want to convert that to a number first, integer to add all the numbers. Now you get the sum. And now you need to return that single digit back, right? If it's a multiple of 10, then you'll return zero. If it's not a multiple of 10, then you need to subtract that number from the next largest multiple number of 10. And this has a lot of a, I mean, there's a couple ways to do this. This is a long way in the school. Also, there's a short way. So well, first of all, we're going to check to see if the sum, right, is divisible by 10, which is a multiple of 10. If that is, is a zero, then we will just basically return zero back. Let me just do that, right? Is zero, then the check digit is indeed zero. And otherwise, if it's not, then it must be differently, right? It must be a different. So therefore we're going to say, if it's, if it's not zero, there must be, you know, other ones. So we can say then the, the check digit is going to be, so I put it for the check digit is going to be equal to, I'll do the shortcut, right? So for example, if it's a 10 and over here, so if it's like a 19, right, to check the check digit, you have to do a 20 subtract 19. But how do we know that it's between 10 and 20? Well, you have to, you can check the sum, right? If the sum is, is less, you know, greater or equal to 10, I mean greater than 10, and less than or equal to 20, there will subtract 20 from, you know, this sum to get the digit. If it's between 20 and 30, you subtract 30 from that. That's a hard way. The short way is, again, it's a multiple of 10. So all you have to do is grab the last digit and then subtract 10 from that, right? So, okay, if I do this, I get nine. And then it's a multiple of 10. So just 10 minus nine, I get one. That's the logic here. So I'm doing is basically 10 minus the sum and used to modules of 10, whatever the number is, subtract that from that, and I get my digit. And once I do that, I'm done. Then, and then here I can return the check digit here. And that's it for this one, right? And so it should be, it should be correct. And of course, if you don't want to create a variable, you could just say return that, and that's fine too. But I'm going to do it. I'll leave it like this. And this is just one way. You could also create a variable called digit up here, and you say digit is zero. Otherwise, digit equals this number and you return digit down here. Either way, again, there's a lot of ways to do it. As long as you have a return statement here, then you should be fine. Okay, so I'll say that. And let's see, do we miss anything else? I think that's pretty much it. So we get the zip code, we get the zip code, pass to the print bar, barcode, we print the four bar at the beginning, we print the four by the end, that's already saved. That for each digit, we're going to print the digit, we go into the print digit function for you up here. Okay, we get the encoded numbers up here, one of these. And for each digit, we're going to build the barcode first. And we're going to build a half bar of four bar, we print that out for each digit, and do that five times, right? Five times zero. After that, we printed the check digit, we did the calculation, we passed entire zip code to that check digit function, calculate check digit function, we did the summation here, we turn it to zero or the check digit, return that here, print it out. Okay, one thing you want to be careful here as you trace this. So in a way I'm tracing my code here, when I pass this digit to the print digit function, it goes in here. And then when I get the code digit, remember it's a digit, it's checked as a string. So that check digit must be in a string format in order for this to work. So that means when I return my check digit, I either return as a string, or I have to convert this to a string down here. So either way you do it, that's fine. So in my case, I would do it here, I would just say return is zero, is a string, or return the string representation, cover that to a string of that digit. And so now I don't have to worry about, you know, conversion out here, you print the digit out, print the code, and we're done. So let's save this and see if this is correct. I'm going to run it. And I'm going to enter the digit we have in the notes over here, it's a 95014, okay, 95014. And let's go back and paste it right in here. All right, so here's the barcode. Let's copy this and bring it back to the document we'll compare to see if it's the same as that or not. And there it is. It looks pretty identical to me, right? So it works perfectly fine. And we can check another digits to make sure another example. So let's say that we do it again, and we're going to enter, you know, five, four, three, two, one. Okay, so we can compare right in here. So five, this is the frame bar. So five is half full, half full, half. So five is half full, half full, half. And then four is, you know, this part here, four is half full, half half full, looks good. And then this is the three, half half full, full half, half full, full half. And then two is half, half, full, half full. And then one is half, half, half, full, full. And then here is the check digit. Is it correct or not? Or if you add five plus four plus three plus two plus one, you're going to get 15, right? You get 15 and subtract 10, subtract five, you're going to get five. So the check digit is a five. And five is half full, half full, half. Half full, half full, half. And the frame here. And there it is. So looks great. So this is one way to solve this problem. I'll show you in another video where I can refactor this a little bit to show you another way to tackle this problem in a different way. So let me know you have questions. Thank you.