我在函數中寫了一個while loop
,但不知道如何阻止它。當它不符合最終條件時,循環會永遠持續下去。我怎樣才能阻止它?如何停止While循環?
def determine_period(universe_array):
period=0
tmp=universe_array
while True:
tmp=apply_rules(tmp)#aplly_rules is a another function
period+=1
if numpy.array_equal(tmp,universe_array) is True:
break #i want the loop to stop and return 0 if the
#period is bigger than 12
if period>12: #i wrote this line to stop it..but seems it
#doesnt work....help..
return 0
else:
return period
問題是你的問題。 「當它不符合最終條件時」。你沒有測試最後的條件,你說的是「雖然真實:」。真實將永遠是真實的。 – 2008-12-15 14:57:19
感謝您的評論,我只是大約一半知道while while循環..所以不真正知道如何問一個好問題.. – NONEenglisher 2008-12-15 15:02:30