2017-02-09 155 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循环。