2016-04-14 5 views
1

私はこのようなものがあると想像してください。 xは毎秒計算され、毎秒異なる値を持ちます。 xが第一の条件を持っている場合、xの値に基づいて、私はXあなたは一度それに入ると状態から抜け出す方法?

if 10 > x > 0: 
    print "It's temporary" 
    do_something(x) 
elif x < 0: 
    print "It gets activated but stay activated" 
    do_something_else(x) 

に別の何かをしたいのは、それが条件2に取得しませんが、私は興味があることは一度xはに行ったということです第2の条件は、たとえxが戻って正になっても、第1の条件には入らず、第2の条件にとどまる。

このようなことを行うためのステレオタイプのアルゴリズムはありますか?

+3

のために適合させることができる表示されますか?あなたは再帰していますか? – miradulo

+0

xは1秒ごとに計算され、毎秒異なる値を持ちます。 xの値に基づいて、私はxと異なる何かしたい。 – auryndb

+3

私はあなたが尋ねていると思っていることを言い直しておきましょう。何回か繰り返してxを評価したいのですが、xが最初の条件を満たさなくなるとすぐに、あなたの 'elif'ステートメントの内容を将来のxの値は? – miradulo

答えて

1

、xが戻って取得し、positive_なった場合は、_even何を意味する、以下の再帰関数は、あなたの目的

def do_something(x, stayActivated = False): 
    if not stayActivated and (10 > x > 0): 
     print "It's temporary" 
     # make an adjustment with said external function 
     do_something(x) 
    elif not stayActivated and x < 0: 
     print "It gets activated but stays activated" 
     do_something_else(x, stayActivated = True) 
    elif x < 0: 
     # x has already been activated and other handling can be applied until any final 
     # condition is met 
2

xが静的​​な値ではない環境に適応できるとすれば、このようなことはうまくいくでしょう。コメントであなたの明確化に基づいて

while 10 > x > 0: 
    print "It's temporary" 
    do_something(x) 
while True: # or something that has a chance of being false 
    if x < 0: 
     print "It gets activated but stay activated" 
     do_something_else(x) 
関連する問題