-3

I'm trying to write a function that switches a variable from True to False, or vice versa. I don't want 2 functions, one for true to false, and one for false to true, I just want one function that toggles a variable between True and False.

var = True
def func(x):
    #CODE
print var #SHOULD PRINT FALSE

any ideas?

DRG
  • 9
  • 3
    what about `return not x` – Pynchia Aug 06 '15 at 21:41
  • possible duplicate of [Use of True, False, and None as return values in python functions](http://stackoverflow.com/questions/9494404/use-of-true-false-and-none-as-return-values-in-python-functions) – Himanshu Mishra Aug 06 '15 at 21:45

2 Answers2

3

If you really want a function, probably the easiest thing to do is

def func(x):
    return not x

var = True
var = func(var)
print var

otherwise simply negate the variable itself

var = True
var = not var
print var
Pynchia
  • 10,996
  • 5
  • 34
  • 43
0
def func(b):
    return not b

Should do it. Returns True if b is False, and vice versa.

Will
  • 4,299
  • 5
  • 32
  • 50