Python – Creating a flow chart using Boolean logic to produce a result, rather than running every line of Python Code!

BoolChart

Important Note – The above flow chart is not something I would imagine is a best practice for a program to keep returning back to the original Conditional “if” every time the program gets input, though I wanted to just throw up a general example.

The above Flow Chart is a very small example of going for a drive in your car, using Boolean True / False (Yes/No) logic, as this is where Boolean shines in Python is to prevent programs from running from beginning to end but finding the desired result before going through the entire program / all lines of code.

This is referred to as “Flow Control” in a Python program, and though some programs will use Loops to continuously run to detect changes and such, some programs simply are looking for the fastest way to resolve an issue based on Yes / No input.

First concept to get right up front: True and False must be in upper case with the rest of the word in lower case or it does not register as a bool value!

For example shown here:

bool1

I could type this all sorts of incorrect way to demonstrate, but just wanted to demonstrate that if it does not start with a cap and rest lower case its wrong!

Comparison Operators that produce a single Boolean result

First a quick note on how Py shares Boolean similarities to Subnetting for Cisco geeks!

It actually works the same was an a “Boolean And” operation with Binary, where you compare an IP Address on top and its Network Mask on the bottom, and 1+1 = 1 while any other value = 0.

Python is the same where:

True + True = True
True + False = False
False + True = False
False + False = False

Shown here is just a quick example demonstrating this in IDLE:

bool2

These Comparison Operators have been used in previous posts of a program I wrote for input / output to determine if someone was of drinking age or just old enough to enter a club, these operators are:

== Equal To
!= Not Equal To
< Less Than
> Greater Than
<= Less than or equal to
>= Greater than or equal to

These were covered lightly in my if / else / elif post, though I kind of feel like I am covering everything lightly with Python and Programming, as there is always a deeper discussion around a subject if you google it and start digging through forums!

If you are not familiar with the operations of these, please visit my original article here that I was playing with these “Comparison Operators” to create a small goofy program.

One thing to remember with this if tested on an exam or reviewing code, keep in mind PEMDAS Mathematical Order of Operation, as I could make 20 different math expressions that you may be is asked if its true or false, for example:

bool3

If you are not familiar with why this is True / False, review my Math Order of Operation article that be viewed in this post where I demonstrate PEMDAS math concepts.

Elements of “Flow Control” and a review of the concepts

My If / Else / Elif article really demonstrates this well but leaves out the exact terminology so I want to review that here.

Flow Control is determined by what are called “Conditions” which in my case was “if” statements that were dependent in the user input to the program to determine the next action, these lead to what are called the “Clause” which are the codes of block that correspond to the Condition as indicated by the Indentation.

Remember that with Python it recognizes chunks of the same code or “Code Blocks” by the level of indentation in the program, where other languages people may do this just to keep it readable, Python only recognizes “code blocks” by their matching indentations!

Some rules of Code Blocks / Indentation

  • The Code Block starts where the indentation starts
  • Code Blocks can contain Code Blocks within them
  • The Code Block ends when the indentation decreases to zero or past matching lines

The code can also be stopped be inserting a ‘Break’ statement in the program, however that has more to do with program Loops, as the ‘Break’ statement essentially stops the program which I’ll touch on lightly with a demo of my same program using 3.x below.

Quick demo of my same else / if / elif program in 3.x to finish off this article

This is by no means conclusive, however I feel like I’m hitting a road block on this subject so I want to finish this article so I can start fresh to demonstrate Loops, though I just got a Raspberry Pi 4 so I might have a geek break this weekend and articles of setting up and playing with that 🙂

Now to the program using Visual Studio Code and Python 3.x code

AgeProg

I’ve throw in some red arrows that are always gotchas to me that I wanted to note here quick so they are not gotchas for you as well, or me when I review this article!

  1. We put rawAge as the input statement, then use age in our if / elif statements, so that it knows to process it as an integer, whereas without the line 7 defining age essentially being a variable for “rawAge” and indicating it is an Integer for input because as discussed all input like this is considered a String unless indicated!
  2. The Colons at the end of the if / elif / else statement, I always forget those, it doesn’t take me long to figure out but know that these statement do require colons
  3. ELSE WILL NOT COMPUTE OPERATORS ! It is the final operator after all other “elif” statements and the final “Well this is the end of this program” kind of line, so it is just “else:” and the following indented code block lines
  4. Important thing I seem to overlook – Note that on the “elif” statement I use the Boolean Operator >= 18, however as demonstrated in my other article (which works the same in 3.x) it will only allow the input to go up to 20, because if someone put in 21 or older it would have hit the first Conditional Statement (if) line!

These are just some common mistakes, remember I am very new to Python and even with 10 different resources, learning programming is just a huge undertaking so I apologize to those reading and thinking “Well duh!” 🙂

That will do it for this session of Python Control Flow with If / Else / Elif!

I just built a Ras Pi 4 so I will probably take a break from the Python Grind and play with that a bit and throw up some posts on here about playing with that / Linux, as I am eager to see what I can do with my new little mini PC.

Until next time!

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 )

Facebook photo

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

Connecting to %s