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

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -