Python simple if or logic statement


How would you write, in python:

if key < 1 or key > 34:

I've tried every way I can think of, and am finding it very frustrating.

8/21/2011 9:11:32 PM

Accepted Answer

If key isn't an int or float but a string, you need to convert it to an int first by doing

key = int(key)

or to a float by doing

key = float(key)

Otherwise, what you have in your question should work, but

if (key < 1) or (key > 34):


if not (1 <= key <= 34):

would be a bit clearer.

8/21/2011 9:24:03 PM

Here's a boolean thing:

if (not suffix == "flac" )  or (not suffix == "cue" ):   # WRONG! FAILS
    print  filename + ' is not a flac or cue file'


if not (suffix == "flac"  or suffix == "cue" ):     # CORRECT!  
       print  filename + ' is not a flac or cue file'

(not a) or (not b) == not ( a and b ) , is false only if a and b are both true

not (a or b) is true only if a and be are both false.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow