-2

I'm having trouble breaking out of the nested while loop in my code completely. How do I break out of two loops? Here's my attempt:

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
klutt
  • 30,332
  • 17
  • 55
  • 95
  • 1
    You have a nested loop, and you want to break out of all of them? Correct? – klutt Apr 04 '18 at 09:41
  • Yes, I want to break out of both of the loops – ShaggyMaster333 Apr 04 '18 at 09:42
  • You can use exceptions for that. I see that you are already using exceptions. – klutt Apr 04 '18 at 09:42
  • Could you show me an example of what to do? – ShaggyMaster333 Apr 04 '18 at 09:45
  • What's the point of `while continue_enter == True or continue_enter == False` ? Do you just mean `while True` ? – khelwood Apr 04 '18 at 09:45
  • Yes! I was just confused and was trying to figure out a way to break out of the loop. Thanks for that – ShaggyMaster333 Apr 04 '18 at 09:47
  • Or you could refactor your code into functions and `return` from them at appropriate points. – khelwood Apr 04 '18 at 09:54
  • You can start with this piece of nonsense: `while continue_enter == True or continue_enter == False:` and how could it possibly evaluate as `False`, but in general having so many nested `while` loops is almost always an indication of a bad code design. You should re-think your approach as this can be greatly simplified using functions. – zwer Apr 04 '18 at 10:04
  • Possible duplicate of [Breaking out of nested loops](https://stackoverflow.com/questions/653509/breaking-out-of-nested-loops) – klutt Apr 04 '18 at 11:04

1 Answers1

0

Here is an example of how to break out of nested loops using exceptions:

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

Output:

$ python prog.py
5
5

You don't HAVE to define an own exception. You can just use the standard ones, or even just use raise Exception('Something happened'). However, I would STRONGLY advice not to do that. See this link for more info: https://stackoverflow.com/a/24065533/6699433

EDIT:

I just read that you can use the builtin exception StopIteration. That seems suitable.

klutt
  • 30,332
  • 17
  • 55
  • 95