我需要执行什么操作才能始终获得False值变为True或获得True值变为False?换句话说,如何切换给定变量的布尔值?
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])
Run Code Online (Sandbox Code Playgroud)
下面的输出仅为True。据此,我了解到由于某种原因我所做的并不是将其他所有值都更改为False。为什么会这样呢?
有谁知道为什么吗?
Light #1 --> True
Light #2 --> True
Light #3 --> True
Light #4 --> True
Light #5 --> True
Light #6 --> True
Light #7 …Run Code Online (Sandbox Code Playgroud)