Is there a way to “break” or “continue” within a Python function? -


i need write function (a flask view) test several conditions request , return message based on tests.

for example…

def index():     test1result = test1(request.form['data'])     if not test1result:         message = 'failed test 1'      test2result = test2(request.form['data'])     if not test2result:         message = 'failed test 2'      test3result = test3(request.form['data'])     if not test3result:         message = 'failed test 3'      return render_template('index.html', message = message) 

if first test fails, don’t want 2nd or 3rd test run , overwrite message. first thought insert return statement each of if blocks, seems messy.

you use elif.

first run tests, , if-elif block

def index():     test1result = test1(request.form['data'])     test2result = test2(request.form['data'])     test3result = test3(request.form['data'])     if not test1result:         message = 'failed test 1'     elif not test2result:         message = 'failed test 2'     elif not test3result:         message = 'failed test 3'      return render_template('index.html', message = message) 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -