22
votes

I just recently joined the python3 HypeTrain. However I just wondered how you can use an if statement onto a boolean. Example:

RandomBool = True
# and now how can I check this in an if statement? Like the following:
if RandomBool == True:
    #DoYourThing

And also, can I just switch the value of a boolean like this?

RandomBool1 == True   #Boolean states True
if #AnyThing:
    RandomBool1 = False   #Boolean states False from now on?
2
Did you try and see what happened? - miradulo
well sure i did states an error :( - Lucidity
"if Check6228 == False: UnboundLocalError: local variable 'Check6228' referenced before assignmen" - Lucidity
You probably did not define your boolean variable then. - Pax Vobiscum
Value assignment uses the = operator, == checks for equality. - miradulo

2 Answers

74
votes

You can change the value of a bool all you want. As for an if:

if randombool == True:

works, but you can also use:

if randombool:

If you want to test whether something is false you can use:

if randombool == False

but you can also use:

if not randombool:
3
votes

I think You could also just use

if randombool is True:

elif randombool is False:

I don't think you need to use equal signs unless it's an int or float.

Correct me if I'm wrong