2016-04-14 47 views
1

想象一下,我有這樣的事情。 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) 

如果x的首要條件,它不進入條件的兩個,但我感興趣的是,一旦X去第二個條件,即使x返回並變爲正數,它也不會進入第一個條件,但會停留在第二個條件中。

是否有任何刻板算法來做這樣的事情?

+3

什麼,如果x回來,併成爲positive_你的意思_even?你在遞歸嗎? – miradulo

+0

x每秒計算一次,並且每秒有不同的值。基於x的價值,我想做一些與x不同的事情。 – auryndb

+3

讓我重新描述一下我想問你的問題 - 你想在迭代次數上評估x,然後只要x不符合你的第一個條件,你就想不斷的執行你的'elif'語句中的內容x的未來價值? – miradulo

回答

1

基於在評論你的澄清,它出現在下面的遞歸功能可用於你的目的

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) 
相關問題