First you put an assignment operator =
instead of the equals operator ==
x = 0
def func(var):
if var == True:
x = x - 1
else:
pass
Second never check if expressions/values are equal(==) to True
/False
especially in python. Just give a statement/variable that is True
or truthy/False
or falsey. In python None(null) empty collections such as [], 0, 0.0, timedelta of zero, and False
are falsey and just about everything else is truthy. To test this for yourself just try passing the value or expression to the bool function to see if it returns True
or False
. See https://docs.python.org/2/library/stdtypes.html#truth-value-testing for more info.
x = 0
def func(var):
if var:
x = x - 1
else:
pass
Third you can lookup but not reassign a variable declared in the same module
outside the function. unless you declare it global. so you can print without global
x = 0
def func(var):
if var:
print x
but not reassign it. Also you don't need a else branch or return statement(python defaults to returning None), so it's just
x = 0
def func(var):
if var:
global x
x = x - 1