2016-10-24 75 views
0

有沒有一種方法可以檢查一個while循環條件在Python循環中是否仍然是真的?類似這樣的:評估while循環在某一點?

i = 0 
while i < 2: 
    i = 2 
    evalcond 
    print("This executes because the condition is still true at this point") 

這可能嗎?

+1

看起來像你想有一個',而1:'和'如果... break'結構。 – TigerhawkT3

回答

1

也許這樣的事情?

i = 0 
while i < 2: 
    i = 2 
    if i >= 2: 
     break 
    print("This executes because the condition is still true at this point") 
0

如果你想避免使用break你也可以做到這一點

i = 0 
while i < 2: 
    i = 2 
    if i < 2: 
     print("This executes because the condition is still true at this point")