title: Python – Breaking out of a deeply nested loop with `while`, `try`, `if`, and `except` statements
body: <p>Here is my code that solves a quadratic equation given user inputs <code>a, b, c</code>. However, I want to check for incorrect user inputs. If a user inputs anything but a float, the user is confronted with <code>print "Not a valid input. Try again.\n"</code> followed by a loop of the function <code>quadratic2()</code>. However, this program is part of a larger program, so I want the user to have the option of typing in <code>"next"</code> to terminate this function and move on. My issue is that I am calling for 3 user inputs, but I want the <code>"next"</code> input to be the first input, <code>a</code>. This works perfectly well until the user types in a random string, causing a <code>ValueError</code>. Once this happens, my code does not want to loop back to check for if the input is <code>next</code>. Please help me! I think something about the way this code is nested is messing me up.</p>
  
  <pre><code>def retest3():
      print "Type in another a, b, and c. Or type \"Next\" to move on."
  def quadratic1():
      print ("This program calculates the zeroes of a quadratic equation."
      "\nPlease enter a, b, and c, hitting \"Enter\" after each one: ")
  def quadratic2():
      while 1 == 1:
          a = (raw_input())
          if "next" == a.lower():
              ontonextthing()
              return 1 == 0
          else:
              try:
                  a = float(a)
              except ValueError:
                  print "Not a valid input. Try again.\n"
                  quadratic2()
              try:
                  b = float(raw_input())
              except ValueError:
                  print "Not a valid input. Try again.\n"
                  quadratic2()
              try:
                  c = float(raw_input())
              except ValueError:
                  print "Not a valid input. Try again.\n"
                  quadratic2()
  
              if b**2-4*a*c&gt;0:
                  root1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
                  root2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
                  print ("First Root: {0}\nSecond Root: {1}".format(root1,root2))
              else:
                  print ("The discriminant is negative, so your"
                  " roots will contain \"i\".")
                  disc1=(b**2-4*a*c)
                  disc2=-disc1
                  sqrtdisc2=(math.sqrt(disc2))/(2*a)
                  b2=(-b)/(2*a)
                  print ("{0} + {1}i".format(b2, sqrtdisc2))
                  print ("{0} - {1}i\n".format(b2, sqrtdisc2))
          retest3()   
  
  quadratic1()
  quadratic2()
  </code></pre>