conditional code

The topic for today is about running conditional code. Conditional code is when you need to check a condition like a value of a variable before the program can continue. For example, the program has to check whether the value of variable is greater or lower than 10. There are a lot of other examples of this in real life and conditions can more and more complicated.

1. Functions

Let’s start with simple example. You may wonder why I tell about functions here but in Python you use a lot of them. A Function allows to reuse code many times while you need to write once (in our case).
I will create a function in which I use conditional code.

Imagine a situation where I would like to go to the cinema but I need to be over 15. An example of such function could look like this.

Take a look in that code:

First, I have defined a function (In [6]) “CanIGoToTheCinema”. This function takes one argument which is an age. As you see there is no need to say a data type of the parameter. I check if the parameter is less than 15, if yes than I show the information that you can’t go to the cinema (“is less than 15. You can’t go!”). If parameter is greater or equal to 15 than the code displays other messages (“is greater or equal to 15. You can go!”).
Once the function is created you can use it. In line 7 (In [7]) I have prepared a variable and then I have called my function in the line 8 (In [8]).
Below you can find an example how I use the function of the different value of the year.

2. Complex conditions

Often the functions are more complicated and you need to check more conditions or complex logic. Imagine a function which is similar to the one we have created but it takes two parameters instead of one. Now the function checks the age (you must be over 15) and if you have enough money to go to the cinema (20 PLN).

I have updated my function so it now takes two parameters (age, money). You can see the definition in line 25 (In [25]). I check the age as in the first example but also check the second parameter. I have used elif instruction to indicate that the other parameter must be checked. The else instruction is only called when the first two checks are not true.
I have tested my function in three ways:
1st: My age was too low to go to the cinema, but I had enough money.
2nd: My age was okay, but I didn’t have enough money.
3rd: My age was okay and I had enough money.
In the first test the output of the function is “12 is less than 15. You can’t go!” which means the first condition was checked.
In the second test the output of the function is “12 is less than 20 PLN. You can’t go!” which means the second condition was checked.
In the third test the output of the function is “18 is greater or equal to 15. 22 is greater or equal to 20. You can go!” which means the last condition was checked.

3. Summary

As you see the conditional code is very important in programming. You will be using it all time. I have shown you how to do this on two examples. I have also created a simple function where the conditional logic is stored.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.