当前位置: 代码迷 >> python >> 返回代码while循环
  详细解决方案

返回代码while循环

热度:51   发布时间:2023-06-13 14:07:51.0

我在使用while循环的简单代码中遇到问题。 我的问题在代码注释中说明。

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

非常感谢。

我认为您正在寻找的是continuebreak语句。

continue将中断当前的迭代(当然,新的迭代将开始)。

break将中断最小的封闭循环。

这两种报表工作for为好。

看看以供参考。

outer = True

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

如果设置outer ,以False的地方,它将结束在下次迭代while循环。

  相关解决方案