从输入和if / else语句运行函数(noob)

时间:2015-11-12 20:13:29

标签: python

我正在学习python,而我正试图创建一个游戏来练习到目前为止我学到的东西。但是,每次输入选项时,无论如何,我的代码都会运行页面的第一个功能。我不明白我做错了什么。

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 个答案:

答案 0 :(得分:2)

emacsclient

被解释为:

emacs --daemon

...因为if "go back" or "way I came" in choice: 是一个非空字符串,所以它与:

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

...并且由于"go back"对于任何x都为True,因此与:

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

这就是为什么你总是要输入True or x构造的那个分支。

你想要的是:

if True: