Home > OS >  When to decide to set up a variable to True or False?
When to decide to set up a variable to True or False?

Time:06-29

I'm trying to figure out why some variables are set up directly with a booleans value.

my_boolean = True
print(my_boolean)

Does anyone have some concrete example to provide and explain the reason for those actions in a real situation? Thanks in advance

CodePudding user response:

Well, in the piece of code below x is set to True(Boolean type) to keep the loop going then in the loop we observe when Val = stop value(10) turn x = false which breaks the loop. here we used x as a way to run a post-condition loop(which is a loop that keeps going until a certain condition is met

import random as r
x = True
stop_value = 10
while x:
    Val = r.randint(1,10)
    print(Val)
    if val == stop_value:
        x = False
print("done")

CodePudding user response:

One scenario I can immediately think of is when attempting to avoid multiple return statements in your functions. Consider this example:

def fcn(string: str) -> bool:
    if string == "abc":
        return False
    return True

To successfully avoid using multiple returns, as seen above, you could provide a result variable with a default value of True. Should the subsequent condition evaluate to True, the variable would change to False and you'd ensure using only one return statement from your function:

def fcn(string) -> bool:
    result = True
    if string == "abc":
        result = False
    return result

It's really a question of readability and personal preference. Opinions are still very much divided on this "single return vs multiple return" topic.

Providing default values can sometimes simplify our code. Other times we just can't do without them at all (see the other answers as well).

Hope that helps.

CodePudding user response:

Usually, boolean values are used to be a means to either establish a condition. Or to check the correctness of a condition. Sometimes they are just simply used to stop an execution from happening further. It has extended usage but the main one is usually to keep a certain value/piece of code in check.

For example, if we need to build grocery list for a household, we have multiple conditions in ourselves to procure the necessary items. If we translate that to a program:

isListCompleted -> A variable that tells us whether we have purchased all the items in the list. (which initially would be False because we haven't purchased anything yet.)

isMoneySufficient -> A variable that tells us whether we have enough money with us to purchase everything in the list. (which would be initially True because we'll have money with us)

So the program can go something like this:

while(isMoneySufficient==True):
if(isListCompleted==False):
purchase(next_item)  // A function that buys next item in the list.
check_balance()  // A function that checks our balance money

Here, two conditions can stop the program, which is,

  1. If the isListCompleted variable becomes True [which can be possible in the purchase function when we won't be able to find any next_item]
  2. If the isMoneySufficient variable becomes False [which can be possible in the check_balance function when we run out of Money.]
  • Related