1

嗨,我在编写这个简单的程序时遇到问题。我刚开始使用 Python,希望能得到一些帮助。当我在程序底部运行 start() 函数时,一切正常,直到第一个 raw_input() 之后。例如,如果用户键入“get coffee”,则打印字符串“Fair enough 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()
4

2 回答 2

4

这个

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

应该

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

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

于 2013-04-18T16:17:58.867 回答
1

你的引擎功能应该喜欢。

def engine(next_scene):
    scenes = {"start":start,"coffee":coffee,"work":work}
    scenes[next_scene]()
于 2013-04-19T10:05:40.907 回答