Exiting out of a program through a menu option
<p>I need to say first and foremost, I am just learning Python.  </p>

<p>I am making a simple python program that has a menu option for exiting the program by using a function I called exit.  I have tried making the exit function just call break, but I am getting an error when the exit function is called.</p>

<p>Any help would be greatly appreciated.</p>

<p>Sorry for not posting code earlier....</p>

<pre><code>def exit():
    break


evade = evade_fw()

# Main program running dialogue
def main():  # menu goes here
    opt_list = [xsl_file,
                basic_loud_scan,
                fw_main,
                exit
                ]
</code></pre>

<p><code>break</code> is for breaking out of <code>for</code> or <code>while</code> loops, but it must be called from within the loop. I'm guessing that you expect the <code>break</code> to break out of your program's main event loop from an event handler, and that is not going to work because, as aforementioned, the <code>break</code> must be within the loop itself.</p>

<p>Instead your exit function can clean up any resources, e.g. open files, database connections, etc. then call <code>[sys.exit()][1]</code> which will cause the Python interpreter to terminate. You can optionally pass a status code to <code>sys.exit()</code> which will be the system exit status available to shell scripts and batch files.</p>

<pre><code>import sys

def exit():
    # clean up resources
    sys.exit()    # defaults to status 0 == success
</code></pre>

python menu exit