string - Python does not skip "if [strg] or [strg] in variable" -


this question has answer here:

i'm writing simple text-based game in python 2.7. in code below, if user answers different "run" or "nothing," arrested() function still kicks in. i'm pretty sure problem in line of code:

if "run" or "nothing" in answer: 

because if provide 1 string, follows:

if "run" in answer: 

the program runs beautifully, calling remote_spot() if answer not contain "run". whole code following:

def facing_cops():     print "there several cars might serve stake-outs cops. do?"     answer = raw_input("> ")     if "run" or "nothing" in answer:         arrested("cops got suspicious. arrested, along drug dealer.")     else:         remote_spot() 

what think matter?

thanks,

chris

the operator 'in' has priority on 'or' you're testing if "run" or ("nothing" in answer). if have two, can write:

if "run" in answer or "nothing" in answer: 

if ever have more keywords test 2, things bit long write , go option:

keywords = ["run","nothing","otherkeyword"] if any(keyword in answer keyword in keywords): 

Comments

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -