Snake Took A Decision : Controlling A Program Continues


In the previous article we took an overview on how we control our program and make it more smarter and helpful for us. We took an overview on the type of statements we use to control our program. In this article we will dicuss first type of statement i.e., Decision Making statements in detail with a story of our stubborn snake. Stroy and imagination makes our knowledge more clear , so let’s begin shall we ?
Snake Got An Offer
Recall the previous article where we read about a stubborn snake kid who is not giving his father’s phone back even after using it for one or two hours straight. After much struggle (What struggle? Just a couple of slaps would do, but no, these parents were born after 1990, and their children are not humans but delicate rose petals that would fly away with a gentle breeze), the snake didn’t stop crying, so his parents decided to give him an offer. If he accepts it, he will return the phone; if he rejects the offer, then make another offer (Did you think he would get punished? Ladies and gentlemen, these new-age parents don’t give punishments to their kids).
So, his parents gave him offers. Let’s see what are those offers and how these offers are connected to the flow of our program.
Offers
The first offer is, if the snake returns the phone immediately, he will get two chocolates. (Please don’t give these offers to your future kids; if you do, your future kid will become greedy at an early age, as the whole world is greedy when they grow up.)
If the snake rejects the first offer, then the second offer is he will get four chocolates.
If the snake rejects the second offer too, then there is another offer of 8 chocolates.
If the snake rejects this too, then there is a fourth offer of 10 chocolates.
If the snake is foolish and his parents are too, then he is getting another offer of 10 chocolates and one ice cream.
Lastly, if the snake rejects all the offers, then the father has to buy a new phone. (As I said, they won’t punish him; their little one would just die, as most parents these days practice bad parenting.)
Flow Chart Of Offers
See in the above image, if the offer is accepted, then our snake returns the mobile phone. Else if his parents give him a second offer, a third, and many more, if he accepts any of those, then he will return the phone. Else, his father has to buy a new phone.
Notice the bold words, these words are keywords in Python. Let’s connect it with our programming world.
Snake Accepts An Offer
Our stupid, stubborn snake kid makes a decision and accepts the fifth offer. That means if he returns the phone, he will get ten chocolates and one ice cream, and he accepts it.
Let’s see how the snake wrote code to make that decision.
Decision In Python
To make such decisions in Python, where you have many choices and have to take one according to the situation (situation here means certain input, certain mathematical condition, etc.), we use decision statements.
Decision Statements
These statements help our code decide what to return as an output based on a certain scenario or condition. There are three decision statements in Python that make our program decision-friendly, meaning it can make its own decisions based on a condition we provide. These are:
If (if)
If is the very first decision statement we use in our program. We cannot use the other two statements without using the if statement. In this statement, we give the initial condition on which our program should decide: if true, then take decision 1; if not true, then decision 2.
Syntax→
if <condition> :
Remember our snake offers, the very first offer is given in the if statement by his parents. Let’s see the code...
Notice, if we directly use elif or else without using if then, our program will give syntax error.
Else-if(elif)
Sometimes we can have more than one condition on which the decision of our program depends. To check more than two conditions, we use the elif decision statement. Remember, it must come after the if statement and before the else statement.
Syntax →
elif <condition>:
Our snake has more than one offer, so his parents used elif to give him more than one offer.
Else(else)
When we have only one condition to check whether it's true or false, we use the else statement right after using the if statement. We cannot use else before if (just like in English grammar, we don’t use else in a sentence before we use if) or elif.
Syntax →
else:
Notice the else statement can’t have a condition to check. Conditions can only be checked in if and elif statements.The else statement only gets executed after the if and elif statements have been executed.
Conclusion
In this article, we explore how decision-making statements in Python are similar to a story about a stubborn snake receiving offers from its parents. By examining if, elif, and else statements, we learn how to structure code to make decisions based on conditions. These statements allow for programming logic that responds to different scenarios, much like the offers presented to the snake.
I hope this story make it very simple for you to understand the decision statements. Let’s continue our journey of controlling the flow of Python program with the next article where we will discuss remaing type of statements.
Thanks for reading. Give me feedback so that I can improve myself. :)
#ChaiCode
Subscribe to my newsletter
Read articles from Nikhil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nikhil
Nikhil
Write code and poems