简体   繁体   English

何时决定将变量设置为 True 或 False?

[英]When to decide to set up a variable to True or False?

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提前致谢

One scenario I can immediately think of is when attempting to avoid multiple return statements in your functions.我可以立即想到的一种情况是尝试避免在函数中使用多个return语句。 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 .为了成功避免使用多个返回,如上所示,您可以提供一个默认值为Trueresult变量。 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:如果后续条件评估为True ,该变量将更改为False并且您将确保仅使用函数中的一个return语句:

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.希望有帮助。

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.好吧,在下面的代码中,x 设置为 True(布尔类型)以保持循环继续进行,然后在循环中我们观察到当 Val = stop value(10) 转 x = false 时会中断循环。 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在这里,我们使用 x 作为运行后置条件循环的一种方式(这是一个一直持续到满足某个条件的循环

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")

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. isListCompleted -> 一个变量,告诉我们是否已经购买了列表中的所有项目。 (which initially would be False because we haven't purchased anything yet.) (最初是 False,因为我们还没有购买任何东西。)

isMoneySufficient -> A variable that tells us whether we have enough money with us to purchase everything in the list. isMoneySufficient -> 一个变量,告诉我们是否有足够的钱购买列表中的所有东西。 (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]如果 isListCompleted 变量变为 True [当我们无法找到任何 next_item 时,这可以在购买函数中实现]
  2. If the isMoneySufficient variable becomes False [which can be possible in the check_balance function when we run out of Money.]如果 isMoneySufficient 变量变为 False [当我们用完 Money 时,这可以在 check_balance 函数中实现。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM