Trying to chain OR operators in an if statement to cut down on redundant code but it seems only the first 2 operators are executed.

what I have now which works, but is messy

if out is "" or out != "sql":
    if out !=  "csv":
        print '\t*** ERROR  **'
    else:
        break
else:
    break

Trying to do similar to the following:

if out is "" or out != "sql" or out !=  "csv":
    print '\t*** ERROR  **'     
else:
    break

If the var is equal to blank, or not equal to sql or csv, then print an error, else continue.

Output {sql|csv}: csv
        *** ERROR **

The above should be != csv (False) and continue to the next line, not printing out the error

2 Answers

2
Andrew Allen On Best Solutions

Another alternative

if a not in ["sql", "csv"]:
1
Community On

This

if out is "" or out != "sql":
    if out !=  "csv":

Is equivalent to

if (out is "" or out != "sql") and (out !=  "csv"):