从另一个功能检查布尔 [英] Check boolean from another function
问题描述
我想,当从其他功能的布尔设置为真做一些事情。我试着用回报(变量),但是当涉及到要求的布尔函数它总是说假。我问这个,因为我觉得这似乎是很基本的东西之前,接过来一看这里。但我无法找到任何有用的。我希望有人能帮助我与此有关。
这里是我的code。
X = 0
布尔=假高清泛函(X,布尔):
如果x为0:
布尔= TRUE
高清functionB(布尔):
打印布尔
如果布尔为True:
打印Halleluhja泛函(X,布尔)
functionB(布尔)打印X,布尔
坚持你怎么写你的code,你有两个选择。选项1是你要修改它使用全局变量,因此务必在功能全球
声明:
X = 0
布尔=假高清泛函(X):
全球布尔
如果x为0:
布尔= TRUE高清functionB():
打印布尔
如果布尔为True:
打印Halleluhja泛函(X)
functionB()打印X,布尔
选项2(preferred)是实际返回的东西,以便它们可以在其他地方传递
X = 0高清泛函(X):
如果x为0:
返回True
其他:
返回False高清functionB(布尔):
打印布尔
如果布尔为True:
打印Halleluhja布尔=泛函(X)
functionB(布尔)打印X,布尔
除此之外,不使用名称布尔
,使用 X == 0
,而不是 x为0
,泛函
可以写成刚返回X == 0
,使用如果布尔:
,而不是如果布尔为True:
,并使用 snake_case
( function_a
),而不是驼峰
。
I am trying to do something when a boolean from another function is set to "True". I tried using return(variable) but when it comes to the function that asks for the bool it always says False. I took a look here before asking this because I felt like this seems to be really basic stuff. But i couldn't find anything usefull. I hope someone can help me with this. Here is my code.
x = 0
bool = False
def functionA(x,bool):
if x is 0:
bool = True
def functionB(bool):
print bool
if bool is True:
print "Halleluhja"
functionA(x,bool)
functionB(bool)
print x, bool
Sticking to how you've written your code, you have two options. Option 1 is to use a global variable, making sure you include the global
declaration in functions where you want to modify it:
x = 0
bool = False
def functionA(x):
global bool
if x is 0:
bool = True
def functionB():
print bool
if bool is True:
print "Halleluhja"
functionA(x)
functionB()
print x, bool
Option 2 (preferred) is to actually return things so that they can be passed elsewhere:
x = 0
def functionA(x):
if x is 0:
return True
else:
return False
def functionB(bool):
print bool
if bool is True:
print "Halleluhja"
bool = functionA(x)
functionB(bool)
print x, bool
Other than that, don't use the name bool
, use x == 0
rather than x is 0
, functionA
can be written as just return x == 0
, use if bool:
rather than if bool is True:
, and use snake_case
(function_a
) rather than camelCase
.
这篇关于从另一个功能检查布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!