2016-10-24 11 views
0

このコードを、iを56とし、iを0に設定することを交互に行わせようとしています.2番目の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) 
+1

このコードは完全ではありません。[mcve] – ForceBru

答えて

1

異議を? elifなし

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

次の2つの別々のifを持っているので、あなたが最初のいずれかを入力しますが、Falseis56を設定し、その直後に第二いずれかを入力し、バックTrueに設定します。最初ifブロック内

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) 
0

変更はすぐに次のいずれかによって逆転されています。その代わり、あなたはelse句を使用することができます。

別のifブロックを1つのif/elseブロックに置き換えます。別のノートで

、あなたは単に、この実装にitertools.cycleオブジェクトを使用することができます。

from itertools import cycle 

c = cycle([0, 56]) 

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

、元のコードは、作業している場合、両方のブロックを実行することになります。どうしてですか:

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

while True: 
    print_i(56) 
    print_i(0) 
関連する問題