2017-02-09 153 views
0

我在while循環的簡單代碼中遇到問題。我的問題在代碼註釋中解釋。返回代碼while循環

CODE

exit = False 
    while not exit: 
     choice = input("Test ") 

     if choice== 1: 
      print "hello" 
      exit = False 

     else: 
      print "good morning" 
      #I want to return to the first while with the input Test but I pass to the second while 
      exit = False 


     exit1 = False 
     while not exit1: 
      choice = input("Test 1") 

      if choice== 1: 
       print "bye" 
       exit1 = False 

      else: 
       print "good evening" 
       #I want to return to the first while with the input Test but I return to the second while 
       exit = False 

非常感謝。

+0

你打算如何退出while循環?因爲您只將false指定爲'exit和exit1'? +你沒有提到什麼是你的問題旁邊告訴你想要什麼,在評論中是不方便的... –

+0

這個劇本應該做什麼? –

+2

就像一個提示:'continue'和'break'語句可以用來在下一次迭代時繼續循環或者打破它。 – Dschoni

回答

0

我想你在找什麼是continuebreak聲明。

continue將中斷當前的迭代(當然,新的迭代開始)。

break會中斷最小的封閉循環。

這兩個聲明也適用於for

看一看here作爲參考。

0
outer = True 

while outer: 
    do_something 
    if a: 
     continue # will get you back to do_something 
    if b: 
     break # will end the outer loop 

如果設置outerFalse的地方,它將結束在下次迭代while循環。