Control Snake Control : Controlling The Flow Of A Python Program

NikhilNikhil
6 min read

Have you ever wondered how our parents controlled us when we were small, stubborn kids? Come on, don’t ask me how this is related to the flow of a program written in the Python language. I will connect it somehow; just focus on the question. You will learn both life and programming lessons from the answer to this question.

Have you ever noticed the behavior of a stubborn kid with his or her parents? How does he or she behave when they want something badly from their parents? How do parents behave with their stubborn kid in these situations? Let’s rephrase this question: How do parents control their kid's stubbornness?

Nowadays, kids are a total mess. It is very difficult to control them. All they want from their first cry on earth is a smartphone and junk food to eat (they don’t want milk nowadays; they just want mayonnaise). It is very difficult for parents to handle such kids, but not impossible.

If you ever notice in your neighborhood (or if you are a parent, then you understand better), parents control their kids by giving them choices and setting conditions based on those choices.

Controling A Stubborn Snake Is Difficult

Let’s understand with an example of our beloved snake. Assume that our snake is a stubborn kid whose parents are 32 years old, basically born in the 1990s. Parents born in the 1990s take care of their kid as if he is a rose petal. They don’t beat him for his mistakes; they give some conditions so that he does something based on those conditions.

The stubborn snake has been watching YouTube shorts for the past hour, and his father needs his phone back to send an important email to the manager.

He asks softly to his son, “Papa ko phone dedo beta, mera pyara baccha h na phone dedo beta” (Please return daddy’s phone, son).

The snake refuses. (If it were my father, he would have slapped me and taken the phone, but these parents were born after 1990.)

His father starts begging for his phone (Again, my father wouldn’t do this), but the snake refuses again.

The father calls his mother; she comes and again starts begging for the daddy’s phone. (My mother would have hit me with a slipper if I didn’t return the phone, but these parents are modern-day parents.)

If you’re wondering why they don’t just discipline their naughty kid instead of making requests, the answer is they are afraid because the snake can file a report against them for domestic abuse of a small kid. (Nowadays, this is happening, sad but true.)

It’s very difficult to take the phone back from the snake’s hands. How do his father and mother proceed to take the phone back from the snake? Let’s see…

Controlling Is Easy Using Conditions

Now the question is how the father of our stubborn snake gets his phone back.

Both parents are in big trouble, but they come up with a great idea.

They discuss with each other, "Why don’t we give him some kind of reward if he returns the phone? That will help, and without any violence, let’s try this."

So, they give a condition to the kid. If the kid accepts that condition, then he will be rewarded; otherwise, no rewards. The reward must be something our stubborn snake cannot live without, or else he will not return the phone.

The conditions are as follows:

  1. If the snake returns his father’s phone now, then our snake will get his favorite chocolates.

  2. If the snake doesn’t return his father’s phone, then he will not get chocolates or dinner.

Mai phir bhi yhi kahunga ki mere mata pita condition nhi chapat lagate or dinner to waise bhi nhi dete wo usme condition ki koi baat hi nhi h.

Now you see how easy it becomes to control our snake. That’s how conditions work in programming as well. Why Control a Program

Let’s start with why our snake’s parents control him. There can be several reasons such as:

  1. To get the phone out of the snake’s hands

  2. To teach the snake how to behave

  3. To instill greediness in the snake’s mind (Indirectly, when we reward him, he becomes greedy for more chocolates, which is not good, to be honest)

Similarly, we are the parents of our program, and we must control it so that our program will do the tasks we want or ask for, not anything else. We need to control our program to:

  1. Make our computer smart.

  2. Perform only required tasks.

  3. Ignore unnecessary tasks.

  4. Perform a task repeatedly for a particular time.

These are some of the reasons why controlling a program is a must if we want to write a program. Let’s see how we control our program.

How To Control A Program

Now imagine that stubborn, dumb snake is your program. You see, all computers and programs are dumb by nature; the flow of control makes them intelligent. We must know how to control a program if we want it to do something helpful for us.

Our snake buddy gets in control because his parents gave him some conditions, and he controlled his actions based on those conditions. But now the question is, how do we control our Python snake, i.e., our Python program?

There are basically three types of statements we can use to control our Python program.

  1. Decision Making Statements

  2. Iterative or Looping Statements

  3. Jump Statements

Decision Making Statements

These statements are the basis for controlling our program. Remember how the snake’s parents gave conditions to our snake? Yes, those conditions are made using decision-making statements.

There are three desicion making statements in Python..

  1. if

  2. elif

  3. else

We will discuss each of them in details in next article.

Looping Statements

If we want to do something repeatedly in our program, we use looping statements. There are two types of looping statements in Python.

  1. for

  2. while

We will discuss them in later articles.

Jump Statements

If we want to jump certain statements within our program, we can do that using jump statements. There are two jump statements in Python.

  1. break

  2. continue

Complete Flow Of A Python Program

Based on my story we can describe the whole flow of our python program.

Here’s a simple Python program for above flow chart.

Conclusion

This article draws an analogy between controlling a stubborn child and controlling a Python program. It explores how parents manage their children's behavior through conditions and rewards, similar to decision-making and control flow in programming. The concept relates to how programmers use decision-making, looping, and jump statements to guide program behavior. By understanding these control structures, we can make our programs perform specific tasks efficiently, much like how conditions can influence a child's actions. The article will further detail these programming concepts in subsequent sections.

Remeber we will discuss each type of statement with same story in detail in upcoming articles..

Thanks for reading, please give feedback if you have any. Share it with other learners..

For Part 2 → click here

#ChaiCode

6
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