1

I am wondering if someone can help me figure out how to fully break out of my while loop(s) and continue with the rest of my program. Thanks!

import time
while True:
    company_name = input("\nWhat is the name of your company? ")
    if company_name == "":
        time.sleep(1)
        print("\nThis is not eligible. Please try again")
    else:
        while True:
            verify_name = input("\nPlease verify that {} is the correct name of your company \nusing Yes or No: ".format(company_name))
            if verify_name.lower() == "no":
                print("\nPlease re-enter your company name.")
                time.sleep(1)
                break
            elif verify_name.lower() not in ('yes', 'y'):
                print("\nThis is an invalid response, please try again.")
                time.sleep(1)
                break
            else:
                print("\nWelcome {}.".format(company_name))
                verify_name == True
                break
        else:
            break
#Continue with rest of my program
GoGoAllen
  • 117
  • 11

1 Answers1

-1

The solution below adds a flag to control when to break out of the external loop that is set to break out each loop, and set back if no break has occurred in the internal loop, i.e. the else statement has been reached on the inner loop.

import time
no_break_flag = True
while no_break_flag:
    no_break_flag = False
    company_name = input("\nWhat is the name of your company? ")
    if company_name == "":
        time.sleep(1)
        print("\nThis is not eligible. Please try again")
    else:
        while True:
            verify_name = input("\nPlease verify that {} is the correct name of your company \nusing Yes or No: ".format(company_name))
            if verify_name.lower() == "no":
                print("\nPlease re-enter your company name.")
                time.sleep(1)
                break
            elif verify_name.lower() not in ('yes', 'y'):
                print("\nThis is an invalid response, please try again.")
                time.sleep(1)
                break
            else:
                print("\nWelcome {}.".format(company_name))
                verify_name == True
                break
        else:
            no_break_flag = True
#Continue with rest of my program

Obviously as you have a condition of while True on the inner loop you will always exit by breaking, but if you had some other condition this would break the external loop only if a break statement was reached on the inner loop.

  • This is overcomplicating matters. The inner loop should not even concern itself with the company name decision loop; it should only test for valid yes / no input. Once that input is obtained, the inner `while` loop should be done, and then the *outer* loop can use the yes / no response to make decisions about the company name. – Martijn Pieters Aug 21 '17 at 20:56
  • I was simply trying to implement a fix with minimal changes to the current code. I can see how your proposal would be a more concise and cleaner solution if we were to rewrite the code more thoroughly though. – Jake Conkerton-Darby Aug 21 '17 at 22:33