i am trying to reassign the value of these booleans from inside of a function and use the reassigned values inside of another function. how do i do that?
JavaScript
x
18
18
1
booleanA = False
2
booleanB = False
3
4
def choose():
5
inPut = input("A or B?: ")
6
7
if "A" in inPut:
8
booleanA = True
9
if "B" in inPut:
10
booleanB = True
11
12
def mainCode():
13
14
if booleanA == True:
15
#do something
16
if booleanB == True:
17
#do something else
18
Advertisement
Answer
In general, you should avoid trying to do this, as it can make your code harder to understand and debug (see action at a distance).
But, if you’re really sure you want to do this, you need to use the global
keyword to tell the interpreter that you’re reassigning to the boolean variables defined outside of the function rather than creating fresh variables:
JavaScript
1
10
10
1
def choose():
2
global booleanA
3
global booleanB
4
inPut = input("A or B?: ")
5
6
if "A" in inPut:
7
booleanA = True
8
if "B" in inPut:
9
booleanB = True
10