Instrucción Python simple if o logic


Cómo escribirías, en python:

if key < 1 or key > 34:

He intentado todo lo que se me ocurre, y me resulta muy frustrante.

Author: Zak, 2011-08-22

3 answers

Si key no es un int o float sino un string, primero debe convertirlo en un int haciendo

key = int(key)

O a float haciendo

key = float(key)

De lo contrario, lo que tienes en tu pregunta debería funcionar, pero{[11]]}

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

O

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

Sería un poco más claro.

 150
Author: agf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-21 21:24:03

Aquí hay una cosa booleana:

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

Pero

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 ) , es falso solo si a y b son ambos verdaderos

not (a or b) es verdadero solo si a y be son ambos falsos.

 8
Author: spikeysnack,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-13 17:54:59

Simplemente puede usar

Si (clave34):

Su problema será resuelto

 0
Author: joginder singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-04 15:49:55