如何完全摆脱嵌套的while循环?

时间:2018-04-04 09:39:02

标签: python loops while-loop

我无法完全摆脱代码中的嵌套while循环。如何突破两个循环?这是我的尝试:

while continue_enter == True:
    while True:
        try:
            enter_result_selection = int(input("What test would you like to enter results for? Please enter a number - Reading Test: 1 Writing Test: 2 Numeracy Test: 3 Digital Literacy Test: 4 All Tests: 5 - "))

            if enter_result_selection == 1:
                name = input("Please enter the name of the student you would like to enter results for: ")
                result_enterer_name = input("Please enter your name: ")                
                while True:
                    try:   
                        student_result_reading = int(input("Enter {}'s percentile for the reading test (%): ".format(name)))
                        break
                    except ValueError:
                        print("This input is invalid. Please enter a percentage ranging from 0 - 100%")

            elif enter_result_selection == 2:
                name = input("Please enter the name of the student you would like to enter results for: ")
                result_enterer_name = input("Please enter your name: ")
                while True:
                    try:
                        student_result_writing = int(input("Enter {}'s percentile for the writing test (%): ".format(name)))
                        break
                    except ValueError:
                        print("This input is invalid. Please enter a percentage ranging from 0 - 100%")

            elif enter_result_selection == 3:
                name = input("Please enter the name of the student you would like to enter results for: ")
                result_enterer_name = input("Please enter your name: ")
                while True:
                    try:
                        student_result_numeracy = int(input("Enter {}'s percentile for the numeracy test (%): ".format(name)))
                        break
                    except ValueError:
                        print("This input is invalid. Please enter a percentage ranging from 0 - 100%")

            elif enter_result_selection == 4:
                name = input("Please enter the name of the student you would like to enter results for: ") 
                result_enterer_name = input("Please enter your name: ")
                while True:
                    try:
                        student_result_digtialliteracy = int(input("Enter {}'s percentile for the digtial literacy test (%): ".format(name)))
                        break
                    except ValueError:
                        print("This input is invalid. Please enter a percentage ranging from 0 - 100%")

            elif enter_result_selection == 5:
                name = input("Please enter the name of the student you would like to enter results for: ")
                result_enterer_name = input("Please enter your name: ")
                while True:
                    try:
                        student_result_reading = int(input("Enter {}'s percentile for the reading test (%): ".format(name)))
                        student_result_writing = int(input("Enter {}'s percentile for the writing test (%): ".format(name)))
                        student_result_numeracy = int(input("Enter {}'s percentile for the numeracy test (%): ".format(name)))
                        student_result_digtialliteracy = int(input("Enter {}'s percentile for the digtial literacy test (%): ".format(name)))
                        break
                    except ValueError:
                        print("This input is invalid. Please enter a percentage ranging from 0 - 100%")
            else:
                print("Sorry, this is an invalid number. Please only enter numbers ranging from the values 1 - 5.")
            break

        except ValueError:
            print("Sorry, your input was invalid. Please enter a number and try again.")

    while continue_enter == True or continue_enter == False:        
        ask_continue_enter = input("Would you like to enter results for another student? ")
        if ask_continue_enter.lower() == "yes" or ask_continue_enter.lower() == "y":
            continue_enter == True
            break
        elif ask_continue_enter.lower() == "no" or ask_continue_enter.lower() == "n":
            continue_enter == False
            break
        else:
            print("Sorry, this is an invalid input. Please enter with 'Yes' or 'No'")
    if continue_enter == True:
        continue
    elif continue_enter == False:
        break

1 个答案:

答案 0 :(得分:0)

以下是如何使用异常中断嵌套循环的示例:

class MyException(Exception):
    pass

x=1
y=1
try:
    while x<10:
        y=1
        while y<10:
            if x==5 and y==5:
                raise MyException
            y=y+1
        x=x+1

except MyException:
    print x
    print y

输出:

$ python prog.py
5
5

您无需定义自己的例外。您可以使用标准版,甚至只使用raise Exception('Something happened')。但是,我强烈建议不要这样做。有关详细信息,请参阅此链接:https://stackoverflow.com/a/24065533/6699433

编辑:

我刚刚读到您可以使用内置异常StopIteration。这看起来很合适。