0

I'm just learning python and I'm trying to create a game to practice what I have learned so far. However, every time a choice is input, my code runs the first function of the page no matter what. I don't understand what I'm doing wrong.

def go_back():
    print "back"

def keep_going():
    print "go"

def help():
    print "help"


def start():
    print """
You have been hiking for hours only to realize you have no idea where you are. \n
What do you do? \n
- Go back the way you came. \n
- Keep going hoping to find and end. \n
- Yell for help.
"""
    choice = raw_input(">")

    if "go back" or "way I came" in choice:
        go_back()
    elif "keep going" or "find" in choice:
        keep_going()
    elif "yell" or "help" in choice:
        help()
    else:
        print "choose one"


start()

1 Answers1

2
if "go back" or "way I came" in choice:

is interpreted as:

if ("go back") or ("way I came" in choice):

...and since "go back" is a non-empty string, that is the same as:

if True or ("way I came" in choice):

...and since True or x is True for any x, that is the same as:

if True:

This is why you will always enter that branch of the if construct.

What you want is:

if choice in ["go back", "way I came"]:
badp
  • 11,409
  • 3
  • 61
  • 89