12

What operation do I need to do to always get a False value to become True or to get a True value to become False? In other words, what do I do to switch the Boolean value of a given variable?

    new_dict = {}
    for i in range(1, 101):
        new_dict[i] = False

    i = 2
    while i < 101:
        for x in range(1, 101):
            if new_dict[x] % i == 0:
                a = new_dict[x]
                new_dict[x] = not a
        i += 1
    for a in new_dict:
        print 'Light #%d --> %r' % (a, new_dict[a])

The output below is only True. From this, I understand that what I did for some reason is not changing every other value to False. Why is this happening?

Does anyone have any idea why?

    Light #1 --> True
    Light #2 --> True
    Light #3 --> True
    Light #4 --> True
    Light #5 --> True
    Light #6 --> True
    Light #7 --> True
    Light #8 --> True
    Light #9 --> True
    Light #10 --> True
    Light #11 --> True
    Light #12 --> True
    Light #13 --> True
    Light #14 --> True
    Light #15 --> True
    Light #16 --> True
    Light #17 --> True
    Light #18 --> True
    Light #19 --> True
    Light #20 --> True
    Light #21 --> True
    Light #22 --> True
    Light #23 --> True
    Light #24 --> True
    Light #25 --> True
    Light #26 --> True
    Light #27 --> True
    Light #28 --> True
    Light #29 --> True
    Light #30 --> True
    Light #31 --> True
    Light #32 --> True
    Light #33 --> True
    Light #34 --> True
    Light #35 --> True
    Light #36 --> True
    Light #37 --> True
    Light #38 --> True
    Light #39 --> True
    Light #40 --> True
    Light #41 --> True
    Light #42 --> True
    Light #43 --> True
    Light #44 --> True
    Light #45 --> True
    Light #46 --> True
    Light #47 --> True
    Light #48 --> True
    Light #49 --> True
    Light #50 --> True
    Light #51 --> True
    Light #52 --> True
    Light #53 --> True
    Light #54 --> True
    Light #55 --> True
    Light #56 --> True
    Light #57 --> True
    Light #58 --> True
    Light #59 --> True
    Light #60 --> True
    Light #61 --> True
    Light #62 --> True
    Light #63 --> True
    Light #64 --> True
    Light #65 --> True
    Light #66 --> True
    Light #67 --> True
    Light #68 --> True
    Light #69 --> True
    Light #70 --> True
    Light #71 --> True
    Light #72 --> True
    Light #73 --> True
    Light #74 --> True
    Light #75 --> True
    Light #76 --> True
    Light #77 --> True
    Light #78 --> True
    Light #79 --> True
    Light #80 --> True
    Light #81 --> True
    Light #82 --> True
    Light #83 --> True
    Light #84 --> True
    Light #85 --> True
    Light #86 --> True
    Light #87 --> True
    Light #88 --> True
    Light #89 --> True
    Light #90 --> True
    Light #91 --> True
    Light #92 --> True
    Light #93 --> True
    Light #94 --> True
    Light #95 --> True
    Light #96 --> True
    Light #97 --> True
    Light #98 --> True
    Light #99 --> True
    Light #100 --> True

The question here How do I get the opposite (negation) of a Boolean in Python? and here python how to "negate" value : if true return false, if false return true seem to be the same but in my case, that simple concept is simply not working...

Thanks, I really appreciate all the help guys!!!

NDKC
  • 123
  • 1
  • 1
  • 7
  • 1
    Logical NOT? You just want to toggle a Boolean value? – Carcigenicate Dec 05 '17 at 19:30
  • 1
    Not sure if this is would be considered a simple typo or not, but I'm pretty sure the answer you're looking for is: replace new_dict[x] % i == 0 with x % i == 0 in your condition... the problem is that False % 2 (or any number) is 0 and True % number is 1. I'm not super pleased with that , but the net effect is that the first time through, you set everything to True and then never change it after... if you do what you almost certainly intended and check the index vs value, it works as I think you would expect, or at least gives mix of False and True values – Foon Dec 05 '17 at 21:56
  • 1
    `if x % i == 0: new_dict[x] = not new_dict[x]` You are requesting module operation on a boolean value, which is not always defined. – Palash Goel Apr 13 '18 at 06:29

3 Answers3

21

Assuming a variable myBool, you can set it with the not keyword.

myBool = True
print(myBool)
myBool = not myBool
print(myBool)

Results in:

True
False
kchason
  • 2,586
  • 15
  • 22
  • @bjohnson Thanks, I know but it still is not working. I added my code to the question. It should be printing something like True, False, True, False, true and so on but it is outputting everything as False. – NDKC Dec 05 '17 at 20:09
2

If you are, for instance, being returned a boolean value from a function, you could do:

bool_value = not my_function()

NOTing the boolean value will invert it to the opposite value. It works because in Python:

>>> not True
False
>>> not False
True

So:

>>> value = True
>>> print(value)
True
>>> print(not value)
False
Totem
  • 6,563
  • 4
  • 32
  • 60
0

Use the not operator:

In [1]: b = True

In [2]: not b
Out[2]: False

In [3]: b = not b

In [4]: b
Out[4]: False

In [5]: not b
Out[5]: True
bjohnson
  • 21
  • 4