Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple 'if' or logic statement in Python [closed]

How would you write the following in Python?

if key < 1 or key > 34: 

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

like image 691
Zak Avatar asked Aug 21 '11 21:08

Zak


People also ask

How do you close an if statement in Python?

Exit an if Statement With break in Python We can use the break statement inside an if statement in a loop. The main purpose of the break statement is to move the control flow of our program outside the current loop.

What are the 3 types of Python conditional statements?

In Python, we can write “if” statements, “if-else” statements and “elif” statements in one line without worrying about the indentation. In Python, it is permissible to write the above block in one line, which is similar to the above block.

How do you close an if statement?

An IF statement is executed based on the occurrence of a certain condition. IF statements must begin with the keyword IF and terminate with the keyword END.


1 Answers

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): 

or

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

would be a bit clearer.

like image 62
agf Avatar answered Sep 30 '22 11:09

agf