python - How to check a variable for certain strings? -
i apologize if i'm not following formatting requirements. first time asking question here , first programming language. topic says, i'd check variable strings. i've tried few options , have researched bit, have still failed find working option.
just give background, i'm trying create word game similar zorg (i think called). i'm doing part of learn python hard way [exercise 35 practice]. anyways, here code. thank time , help!
#first room, allows 3 options def firstroom(): print "you have begun test." print "for sake %s, hope pass." % name print "------------------------------------" print "you enter new room , see 3 paths." print "the middle path illuminated eery blue light." print "the left path illuminated dangerous red light." print "the right path illuminated soft green glow." next = raw_input("what path take?" ) if next in ("left", "red"): print "you cautiously approach left path." print "before can react, axe swings down , kills you." exit("thank %s playing, unfortunately died." % name) elif next in ("middle", "straight", "blue"): print "a smart choice. head down middle path." print "as approach end of middle path, see blue light." blueroom() elif next in rightpath: print "you lured green light." else: print "command not recognized." firstroom()
there no declaration of variable rightpath
in function. otherwise, code looks if behaves intend to.
my suggestion change:
elif next in rightpath
to
elif next == rightpath
assuming rightpath
holds single string
value.
edit
to achieve described in comment, should split user input (which potentially sentence) list of words:
next = raw_input("what path take?" ).split()
each comparison have change incorporate any
function:
if any(item in next item in ("left", "red")): ... elif any(item in next item in ("middle", "straight", "blue")): ... else: ...
any
return true
word in user input exists in expected list of responses given case.
Comments
Post a Comment