2017-06-06 8 views
-4

は、私はフォーマットのこの種で何かを持っていると言う:以前にネストされた `while True`ループに戻るには?

while True1: 
    if something: 
     do some thing 
    elif something else: 
     do something else 
     while True2: 
      if something1: 
       do some thing1 
      if something2: 
       do some thing2 
      if want to end this while True2 loop 
       go back to first while True1 loop 
    elif something else else: 
     do some thing else else 

など

どのように私はwhile Trueループ内の何かが、それは内部にネストされたことを以前while Trueループに戻ることができますか?

+9

'break'は電流ループから抜け出します – fechnert

答えて

2

break文はまさにそれを行い終了します。

while True: #1 
    if something: 
     do something 
    elif something else: 
     do something else 
     while True: #2 
      if something1: 
       do something 
      if something2: 
       do something else 
      if want to end this while True #2 loop: 
       break   # will break out of the innermost loop only 
    elif something else else: 
     do some thing else else 

続きを読む:Control Flow Tools

3

breakステートメントを使用してください。

x = 0 
while True: 
    x += 1 
    if x == 5: 
     print(x) 
     break 

意志出力5

関連する問題