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?
booleanA = False
booleanB = False
def choose():
inPut = input("A or B?: ")
if "A" in inPut:
booleanA = True
if "B" in inPut:
booleanB = True
def mainCode():
if booleanA == True:
#do something
if booleanB == True:
#do something else
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:
def choose():
global booleanA
global booleanB
inPut = input("A or B?: ")
if "A" in inPut:
booleanA = True
if "B" in inPut:
booleanB = True