如何用 Python 编写下面的代码?
if key < 1 or key > 34:
我已经尝试了所有我能想到的方法,但是我发现这非常令人沮丧。
If key isn't an int or float but a string, you need to convert it to an int first by doing
key
int
float
str
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):
or
if not (1 <= key <= 34):
would be a bit clearer.
Here's a Boolean thing:
if (not suffix == "flac" ) or (not suffix == "cue" ): # WRONG! FAILS print filename + ' is not a flac or cue file'
but
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 (not b) == not ( a and b )
not (a or b) is true only if a and be are both false.
not (a or b)