当另一个函数的布尔值设置为“True”时,我正在尝试做一些事情。我尝试过使用return(variable),但是当涉及到请求bool的函数时,它总是说False。在问这个之前我看了一下这里,因为我觉得这似乎是很基本的东西。但我找不到有用的东西。我希望有人能帮我。 这是我的密码。在
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
首先,不要使用名为
bool
的变量。它由Python保留,例如str,list,int
等其次,
bool
在全局范围内,因此如果要在函数中编辑它,必须将其定义为global
。在输出
^{pr2}$坚持写代码的方式,你有两个选择。选项1是使用全局变量,确保在要修改的函数中包含
global
声明:选项2(首选)是实际返回内容,以便将它们传递到其他地方:
^{pr2}$除此之外,不要使用名称},},使用}。在
bool
,使用x == 0
而不是{functionA
可以写成return x == 0
,使用if bool:
而不是{snake_case
(function_a
)而不是{https://docs.python.org/3.4/tutorial/controlflow.html#defining-functions:
对}。在
functionA
中的bool
的赋值将在functionA
中创建一个局部变量;它不会分配给全局变量{相关问题 更多 >
编程相关推荐