Python 我这里的简单代码有什么问题?

Python 我这里的简单代码有什么问题?,python,python-2.7,Python,Python 2.7,嗨,我在写这个简单的程序时遇到了问题。我刚开始学习Python,希望能得到一些帮助。当我在程序底部运行start函数时,在第一次原始输入之前,一切正常。例如,如果用户键入get coffee,则会打印字符串Fair event take a break,但在此之后,它不会像我所希望的那样运行coffee函数,而是再次循环到start函数 有人能帮忙吗? 非常感谢 def engine(next_scene): scenes = {"start":start(),"coffee":coff

嗨,我在写这个简单的程序时遇到了问题。我刚开始学习Python,希望能得到一些帮助。当我在程序底部运行start函数时,在第一次原始输入之前,一切正常。例如,如果用户键入get coffee,则会打印字符串Fair event take a break,但在此之后,它不会像我所希望的那样运行coffee函数,而是再次循环到start函数

有人能帮忙吗? 非常感谢

def engine(next_scene):
    scenes = {"start":start(),"coffee":coffee(),"work":work()}
    return scenes[next_scene]

def start():
    print "you are in the office"
    print "you wonder what to do"
    action = raw_input("what do you do? Get coffee or work?")

    if action == "get coffee":
        print "Fair enough take a break"
        next_scene = "coffee"
        engine(next_scene)
    if action == "work":
        print "Good man, you are well on your way to being a coder"
        next_scene = "work"
        engine(next_scene)

def coffee():
    print "You walk out of the room"
    print "You head down the stairs and into the cafe"
    print "You order an espresso"
    print "You neck it down"
    print "Yumm"
    print "You are wired"
    action = raw_input("Now what? Work or go home? > ")

    if action == "work":
        print "You head back upstairs"
        next_scene = "work"
        engine(next_scene)
    if action == "go home":
        print "You lazy git"

def work():
    print "You beaver away and become a cool coder"
    next_scene = "start"
    engine(next_scene)

start()
这个

应该是

scenes = {"start":start,"coffee":coffee,"work":work}
您调用了字典定义中的函数,但只想获取函数对象。

应该是

scenes = {"start":start,"coffee":coffee,"work":work}

您调用了字典定义中的函数,但只想获取函数对象。

您的引擎函数应该是这样的

def engine(next_scene):
    scenes = {"start":start,"coffee":coffee,"work":work}
    scenes[next_scene]()

你的引擎功能应该是这样的

def engine(next_scene):
    scenes = {"start":start,"coffee":coffee,"work":work}
    scenes[next_scene]()

我认为您希望删除场景字典中的after-your函数。包含括号尝试调用这些函数。我认为您希望在场景字典中删除函数后的。包括括号试图调用这些函数,OP可能想把下面的行改为:returnscenes[next_scene]当然可以。他必须注意不要将对engine.@Mathias的调用堆叠起来。谢谢你的帮助。对不起,但是如果下一个场景=coffee,我该如何实际调用函数coffee?很抱歉问了这么多基本的问题。@Reno:看看Jon Clements的评论。或者执行类似于func=enginext\u scene,后跟func的操作。@JonClements谢谢,我现在知道如何调用该函数了。OP可能想将下面的行更改为:return scenes[next\u scene]当然可以。他必须注意不要将对engine.@Mathias的调用堆叠起来。谢谢你的帮助。对不起,但是如果下一个场景=coffee,我该如何实际调用函数coffee?很抱歉问了这么多基本的问题。@Reno:看看Jon Clements的评论。或者执行类似于func=enginext\u的操作,然后执行func。@JonClements谢谢,我现在知道如何调用该函数了。