The conditional… or “IF” statement
Programs that count really fast or create MADLIBs are a ton of fun, but part of encoding knowledge is the ability to make decisions in code. To do that we need something that will allow the code to take two different paths… the “if” statement.
The if statement checks a value for truth. If it finds it, then it will execute the commands if owns. If it doesn’t find truth, its code is not allowed to be run.
Let’s try it…
There is another program available in the kidpi git repository that will let us play with that concept:
cd cd /kidpi/python ls
bath.py papirus-btc pong.py count.py papirus.service stinky.py
Run that program:
I wonder what would have happened if I answered the OTHER way! Lets try it:
Hey! So now the program is able to do two different things based on the user’s input!
So what’s happening there. Lets take a look.
Edit the code…
You can see it builds on the program “stinky.py” we played with earlier… but now there is that interesting question its asking… lets take a look:
See the if statement? That’s the new piece of magic we are using. It takes the variable we created called isStinky, pulls out its value, and checks it against the right hand side. In this case y.
Comparison Operators… ==, >, <, >=, <=, !=
There are some other hidden gems in that if statement. The == in that line is funky too. That tells the ‘if’ statement to check if things are equal before saying that is true.
(Why not use “=”? Because that is the bit of code reserved for “assignment” or “giving a variable a value”, so the folks who built the language couldn’t use it for truth testing.)
Comparison Operators… or Relational Operators are special things used in if statements to give you the ability to ask:
- Is it equal?
- Is it greater than?
- Is it less than?
- Is it less than OR equal?
A little copy and paste…
So now lets add another question to the code. How about “Do you have soap?”
To make this easy we’re going to learn how to copy and paste in VIM.
Move your cursor to the line:
Now type ESC SHIFT-V (where the SHIFT part here is the shift key), which will start a “highlight block”, then use your arrow keys to move the cursor all the way down below the print statement:
Now hit y and the code will be copied into a memory buffer inside VIM’s memory banks.
Move your cursor below the last print statement like this:
And hit p for “paste”. Which will paste or place the contents from that memory bank at the place of the cursor, like this:
Now lets make some changes… using the x key delete isStinky, and replace it with hasSoap by hitting the i key and typing in the the new name.
Then do the same for the question and the if and the print statements, until you have something like this:
And the whole program looks like this:
Now lets run it and see what happens!
Huzzah! We now have a program that has asked two questions! Now you can do a full questionnaire! Simple programs like this can help people or robots make decisions!
Try adding a couple more questions!
2 thoughts on “Lessons on PI – Lesson 3 (cont.): A little programming…”