Python If语句未触发

Python If语句未触发,python,if-statement,Python,If Statement,我试图使此代码在将I设置为56和I设置为0之间交替进行。我似乎无法触发第二个if语句。第一个有效 while True: print 'is55 before if logic is' + str(is56) if is56 == True: i = 0 is56 = False #print 'true statement' + str(i) print 'T

我试图使此代码在将
I
设置为56和
I
设置为0之间交替进行。我似乎无法触发第二个if语句。第一个有效

  while True:
        print 'is55 before if logic is' + str(is56)
        if is56 == True:
            i = 0 
            is56 = False 
            #print 'true statement' + str(i)
            print 'True is56 statement boolean is ' + str(is56)
        if is56 == False:   
            i = 56 
            is56 = True                
        print  'i is ' + str(i)

您有两个独立的
if
,因此输入第一个,将
is56
设置为
False
,然后立即输入第二个并将其设置回
True
。相反,您可以使用
else
子句:

while True:
    print 'is55 before if logic is' + str(is56)
    if is56:
        i = 0 
        is56 = False 
    else: # Here!
        i = 56 
        is56 = True                
    print  'i is ' + str(i)

您有两个独立的
if
,因此输入第一个,将
is56
设置为
False
,然后立即输入第二个并将其设置回
True
。相反,您可以使用
else
子句:

while True:
    print 'is55 before if logic is' + str(is56)
    if is56:
        i = 0 
        is56 = False 
    else: # Here!
        i = 56 
        is56 = True                
    print  'i is ' + str(i)

第一个
if
块中的更改立即被下一个块反转

您希望将单独的
if
块替换为单个
if/else

另一方面,您可以简单地使用对象来实现:

from itertools import cycle

c = cycle([0, 56])

while True:
    i = next(c)
    print  'i is ' + str(i)
    # some code

第一个
if
块中的更改立即被下一个块反转

您希望将单独的
if
块替换为单个
if/else

另一方面,您可以简单地使用对象来实现:

from itertools import cycle

c = cycle([0, 56])

while True:
    i = next(c)
    print  'i is ' + str(i)
    # some code
有反对意见吗

while True:
    print 'is55 before if logic is' + str(is56)
    i = is56 = 0 if is56 else 56             
    print  'i is ' + str(i)
有反对意见吗

while True:
    print 'is55 before if logic is' + str(is56)
    i = is56 = 0 if is56 else 56             
    print  'i is ' + str(i)

如果没有
elif
,原始代码(如果工作)将执行两个块。为什么不:

def print_i(i):
    print 'i is ' + str(i)

while True: 
    print_i(56)
    print_i(0)

如果没有
elif
,原始代码(如果工作)将执行两个块。为什么不:

def print_i(i):
    print 'i is ' + str(i)

while True: 
    print_i(56)
    print_i(0)

此代码不完整,请发布此代码不完整,请发布