The next thing that we will talk about in this book is how to create the decision control structure inside of Python. There may be situations when you write out your code and you need it to make a decision for you. This could happen when you are working on a game or some other option that needs an input from the user. You may want to have an answer show up only when your user puts in the right answer and other times you may pick to have an answer come up whether the input is what you require or another answer so that the user has some choices. All of this can be done with the decision control structures in Python and you would be able to set up the conditions that need to be met before this all works.
To start out with this toic, we are going to keep it pretty simple and start out with the if statement. This is the simplest form of the decision control structures because it is only going to work if the user puts in the right input, but if the user places in the wrong input based on your conditions, nothing will show up on the program. There are limitations to using this, but it is a good place to get started to see how they work. Below is an example that you can practice on when first learning how to do the if statements.
Age=int(input(“Enter your age:”))
if (age<=18):
print(“You are not eligible for voting, try next election!”)
print(“Program ends”)
With the code above, there are a few options that can happen. If your user comes in and states that they are 18, or under, your program is going to work and will show the message “You are not eligible for voting, try next election!”. After this shows up on the screen, the program is going to end, or at least will move on to the next part of the code. But there are some issues if the user puts in their age as 30 or another number higher than 18. They can put in any age that they would like and it isn’t wrong, but since it doesn’t meat the conditions that you set above, it isn’t going to give them a result.
If your user places in an answer of 25, the program is going to see that this doesn’t equal the conditions that you set out. The program will simply end at this point. There may be some applications where it is just fine to have just the one answer, but many times you want to make sure that the program is set up to handle any kind of answer that your user gives you. You don’t want to have the program just shut down when the user puts in the answer that is seen as false, you want to make sure that the program is going to show up something. This is the limitation of using the if statement, but there is a nice solution that can work well to fix this issue.
When you run into the issue of needing the code to respond no matter what answer the user gives to you, you will want to use the if...else statements. You can set it up to handle the example above whether the user puts in their age as 18, like what happens above, or if they answer that their age is above 18. This makes it easier for the user to place in their information, no matter what it is, and still get the answer that they want. Let’s look at the way that the if...else statement will work in Python:
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”)
With this option, there are two different possibilities that will come up. You will notice that if the user states that they are younger than 18, the first statement will show up and tell them that they aren’t eligible to vote in the election. But if they state that they are older than 18, such as 21, they will get the second statement that we put into the code. These statements can be modified to use any way that you would like, which helps to make it more customizable, but it will all work the same. This is going to make it easier for your user to get the right response based on their age no matter what age they put in.
Now, this is a simple if...else statement that you can work with and you can go through and add in some more possibilities to this as well. For example, if you would like to split up the answers into three or four groups, rather than just the two, you would be able to do this with more of the else parts into the code. You could have a message come up for people who are 16 to 18, and then one for those who are 19 to 25, and another for those who are 26 and over if you would like and depending on the code that you are working on. There are so many possibilities here, you just need to determine how you would like to split it all up and then add in the right parts, and the right message that should come up on the screen, to make this happen.
Using the elif statements
Above we worked on the if and the if...else statements, both of which can be really nice if you are working on adding some more options to your code and accepting the answer in a certain way based on what the user gives to you. But there are times when you would want to do something a little differently and let the user pick from a list of options that are on the screen. If you would like to make your code do something like this, then the elif statement is the right one for you.
You are able to write out as many of the elif statements as you want as long as you do it properly and it fits into your code the right way. Sometimes you may just want to have two options available, but then there are the codes that would have twenty options available in ordder to make it work out the right way. You can add in as few or as many of these as needed for your code, just make sure that you write out enough of the elif codes in your statements and the right information to go with them to ensure that the code will work properly. Here is a good look at one example of working with the elif statements so you get a good feel for how they should work:
Print(“Let’s enjoy a Pizza! Ok, let’s go inside Pizzahut!”)
print(“Waiter, Please select Pizza of your choice from the menu”)
pizzachoice = int(input(“Please enter your choice of Pizza:”))
if pizzachoice==1:
print(‘I want to enjoy a pizza napoletana’)
elif pizzachoice==2:
print(‘I want to enjoy a pizza rustica’)
elif pizzachoice==3:
print(‘I want to enjoy a pizza capricciosa’)
else:
print(“Sorry, I do not want any of the listed pizza’s, please bring a Coca Cola for me.”)
Now when you use this option, your user is able to go through and make the choices that they would like to have and they simply would pick the number that goes with it. For example, if they picked number three, they would be ordering the pizza carpicciosa and so on inside this program. Here we just picked out three options and then had a default at the end that was able to catch anything that didn’t fall into those three categories or for those who would rather have a drink instead of pizza. You can expand this out, if it works for your code, and add in ten types of pizza that the user is able to pick from if you would like, but this is a good simple example that shows how this will work.
The decision control statements in Python can add in so much more to your code. Rather than just having something that shows up on the screen, you can use these control statements in order to have some kind of interaction with the user while they are on the program. You can keep it simple, like with the if statement, and only let there be one answer that is considered true or you can make it more difficult and add in several different options based on the answer or let the user pick the result that they want. These decision controls are pretty basic for you to learn how to use, but they can add in so much power when it comes to how well your codes will work for you!