Are decisions in Control Flow required to be binary, as in "Yes" or "No"?

Question

Are decisions in Control Flow required to be binary, as in, only evaluated to “Yes” or “No”?

Answer

In Control Flow, every decision can only be answered as either “Yes” or “No”, so they must be binary.

Python Control Flow works by utilizing boolean expressions, which are expressions that evaluate to either True (Yes) or False (No). These expressions are used in statements such as if statements which will run their code if the expression is true, or will not run their code if the expression is false and continue checking the next decision. Control Flow ‘flows’ down through each binary decision in the path until it reaches the end.

17 Likes

Python will cast values when they don’t quite fit the situation. “Yes” becomes True by virtue of not being the empty string.

11 Likes

It checks through all the statements (in order) until it’s output becomes “True”, when it reaches statement where it is true, it will do the code you assign it to do
For example

my_number = 12

if my_number > 20: <— False
print: “your number is more than 20”

elif my_number == 20: <— False
print: “your number is 20”

elif my_number < 20 <— True
print “your number is less than 20”

because that one evaluates to true, it says “your number is less than 20”

3 Likes

If x is not greater than 20 and x is not equal to 20, then x must be less than 20.

else:
    print “your number is less than 20”
5 Likes

What about fuzzy logic ?? Does base python support that ??

1 Like

I’m not an expert in Python but fuzzy logic is not included into the basic install of Python. You will need to install it with pre-installations of
NumPy >= 1.6,
SciPy >= 0.9, and
NetworkX >= 1.9.

To install use:
$ pip install -U scikit-fuzzy

2 Likes

The “correct” code would be the following:

my_number = 12

if my_number>20:
    print('your number is more than 20')
elif my_number==20:
    print(f'your number is {my_number}')
else:
    print('your number is less than 20')
5 Likes

I think that from a design perspective, having binary decisions only would force us to slice the code into simple pieces, as simple as a “yes/no” decision.

The decision in the post above yours is ternary, yet gives a usable outcome. Put design perspective aside and think logic.