2017-02-09 15 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ループをどのように終了する予定ですか?あなたは 'exitとexit1'にのみfalseを割り当てているので? +あなたは、不便なコメントにあなたが何をしたいかを伝えることのほかにあなたの問題は何か言及していませんでした... –

+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 

どこかFalseouterを設定した場合、それは次の反復でwhileループを終了します。