How to Preparing Your Self for Coding

Another fun thing to work with when you are in the conditional statements, these are going to be known by a lot of different names, such as “if” statements and the decision control statements. But they are going to be a great option when you would like the program to learn how to do a few things on its own, without you having to think about all of the possible inputs before you even start. There are going to be times when you would like to make sure that your code behaves in the right manner and can make some decisions on its own when you are not able to be there to monitor it all and hope that it all fits into the right place. Any time that you have a part of your code that will allow the user to put in any kind of answer that they want all on their own, rather than just selecting from a few options, then you are going to find that the conditional statements are the best ones to work with.

In this phase, we are going to take a look at the three most common options of the conditional statements that you are likely to use with some of your coding. The three that we are going to focus on the most are the “if” statement, the if-else statement, and the “elif” statement. These will all work in a slightly different manner from one another, but they can all add some great things to your code, so we are going to spend our time taking a look at them and how they are going to work for our needs.

The “If” Conditional Statements

The first thing we are going to look at is regular “if” statement. This keeps things simple and will ensure that we are set and ready to handle some of the basics of these conditional statements. This one is based on the idea that the answer the user gives is true or it is false, depending on what conditions you have set. If the user adds in input that the program is going to see as true, then your program will see this and will continue on to the next step. But if the user does put in an answer that is seen as false for that part of the code, then the program will just end because nothing is set up to handle this issue along the way. As we can see here already, there is the potential for some problems when you are working with this kind of coding. But we are still going to take a quick look at this to see how it works and to get the basic idea of these conditional statements, and then move on to how we can change things on to fix this issue. A good example of how the, if the statement is able to work, will be below:

age = int(input(“Enter your age:”)) if (age <=18):

print(“You are not eligible for voting, try next election!”)

print(“Program ends”)

Let’s explore what is going to happen with this code when you put it into your program. If the user comes to the program and puts that they are younger than 18, then there will be a message that shows up on the screen. In this case, the message is going to say, “You are not eligible for voting, try next election!” Then the program, as it is, is going to end. But what will happen to this code if the user puts in some age that is 18 or above?

With the “if” statement, nothing will happen if the user says that their age is above 18. The “if” statement just has one option and will focus on whether the answer that the user provides is going to match up with the conditions that you set with your code. The user has to put in that they are under the age of 18 with the “if” statement in this situation, or you won’t be able to get the program to happen again.

As we have already mentioned with this one, the “if” statement could end up causing us a few problems when we are coding. You want to make sure that the user is able to put in any answer that is the best for them, not the “right” answer, and you want to make sure that the program you are writing is still going to be able to respond and give some kind of answer to the user along the way. Some of the users who come to your website or program will have an age that is higher than 18, and it is going to be confusing and look bad if they put that answer in and can’t get the program to work.

The If-Else Conditional Statement

This is why we are going to move on to the if-else statement. This one is used a lot more often than we see with the “if” statement, and it is able to handle some of the problems that we saw with the “if” statement. This kind of statement is going to work with some of the topics that we had above and make some changes to fix the issues and ensure that this all works.

Let’s say that we are still working with the same kind of program that we had above. But this time, we want to make sure that we have some kind of result show up on the screen, no matter what answer the user inputs into our program this time. So, with this one, we are going to work to separate out the users based on their age. There will be a group that is above 18 and one that is under 18, and a response from the system based on this. The code that we would be able to use to help us write out our own “if else” statement is going to be below:

age = int(input(“Enter your age:”)) if (age <=18):

print(“You are not eligible for voting, try next election!”)

else print(“Congratulations! You are eligible to vote. Check out your local

polling station to find out more information!) print(“Program ends”)

As you can see, this really helps to add some more options to your code and will ensure that you get an answer no matter what results the user gives to you. You can also change up the message to say anything that you want, but the same idea will be used no matter the answer that the user gives. You have the option to add in some more possibilities to this. You are not limited to just two options, as we have above. If this works for your program, that is just fine to use. But if you need to use more than these two options, you can expand out this as well. For example, take the option above and expand it to have several different age groups. Maybe you want to have different options come for those who are under 18, those that are between the ages of 18 and 30, and those who are over the age of 30. You can separate it out in that way, and when the program gets the answer from the user, it will execute the part that you want. The cool thing with this is there are a lot of different options and programs that we are able to write that work with this. Maybe we want to create our own program that allows the user to go through and pick out one of their favorite types of candy. There are a ton of different types of candy, and they go by so many different names that it is really hard to list them all out and be prepared for this ahead of time. But the “if else” statement would be able to help us to handle all of this.

With this one, we would just pick out a certain number of candy choices that we would like, maybe the top six, and then list out a response that goes with that. And then, we would use “if else” statement at the end in order to catch all of the answers that did not fit in with the original six that we listed out. This ensures that no matter what input the user adds to the system, they are going to get some kind of response out of the process as well. The else statement in all of this is going to be an important thing to make sure it is there because it is responsible for catching all of the answers that are left that the user could potentially give to you. If you don’t have this statement placed in the code, or not in the right part of the code, it is not going to be able to catch all of the other possible inputs of the user as you would like.

Now that we have had a chance to talk about the “if” statement and the “if else” statement, it is time for us to move on to our “elif” statements. These are a unique part of programming in the Python language, and they are going to help us add in another level to some of the conditional statements that we are able to work with. This kind of conditional statement is going to allow for a user to pick out a few choices that you present to them, and then, depending on what answer or choice the user goes with, the program is going to execute the code and provide the results that go with that answer.

The Elif Conditional Statement

You will find that these “elif” statements are going to show up in a lot of different places. One option is going to be when they show up in the games that you play. If you have ever gone through and played a game or been on another kind of program where you are given a menu style of choices to make, then you have already had some experience with these “elif” statements doing their work.