Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple conditions with if/elif statements [duplicate]

I'm trying to get an if statement to trigger from more than one condition without rewriting the statement multiple times with different triggers. e.g.:

if user_input == "look":  
    print description


if user_input == "look around":
    print description

How would you condense those into one statement?

I've tried using 'or' and it caused any raw_input at all to trigger the statement regardless of whether the input matched either of the conditions.

if user_input == "look" or "look around":  
    print description
like image 523
Blaine Avatar asked Sep 08 '12 23:09

Blaine


People also ask

Can you use Elif twice?

The only strict limit is that there can only be one if and one else per if/elif/else statement block, but there is no limit on the number of elif .

Can you use Elif twice in Python?

There can be multiple 'elif' blocks, however there is only 'else' block is allowed. 2. Out of all these blocks only one block_of_code gets executed. If the condition is true then the code inside 'if' gets executed, if condition is false then the next condition(associated with elif) is evaluated and so on.

How is using multiple IF statements different than using an Elif?

Multiple 'if' statements get checked one after another with no regard to the former's results. 'Elif' acts as 'else if', so is checked only in the 'else' case - that is only then, if the first 'if' is False.

Do all conditionals with Elif statements need an else statement?

IF, ELSE or ELIF (known as else if in some programming) are conditional statements which are used for execution of different code depends on condition. The if statements can be written without else or elif statements, But else and elif can't be used without else.


2 Answers

What you're trying to do is

if user_input == "look" or user_input == "look around":
    print description

Another option if you have a lot of possibilities:

if user_input in ("look", "look around"):
    print description

Since you're using 2.7, you could also write it like this (which works in 2.7 or 3+, but not in 2.6 or below):

if user_input in {"look", "look around"}:
    print description

which makes a set of your elements, which is very slightly faster to search over (though that only matters if the number of elements you're checking is much larger than 2).


The reason your first attempt always went through is this. Most things in Python evaluate to True (other than False, None, or empty strings, lists, dicts, ...). or takes two things and evaluates them as booleans. So user_input == "look" or "look around" is treated like (user_input == "look") or "look_around"; if the first one is false, it's like you wrote if "look_around":, which will always go through.

like image 134
Danica Avatar answered Nov 14 '22 16:11

Danica


You could use regular expressions to match the strings if they follow a pattern with optional sections or you could do an array lookup:

if user_input in ["look", "look around"]:
    print description

The boolean operator or only works with boolean values, it evaluates the expressions on both sides and returns True if one of the expressions evaluates to True. It has nothing to do with the natural language '

like image 24
nemo Avatar answered Nov 14 '22 16:11

nemo