我的简单代码出了什么问题?

时间:2013-04-18 16:16:06

标签: python python-2.7

嗨,我在编写这个简单的程序时遇到了问题。我刚开始使用Python并希望得到一些帮助。当我在程序的底部运行start()函数时,一切正常,直到第一个raw_input()之后。如果用户键入"获取咖啡"例如,字符串" Fair足够休息"是打印但在此之后,而不是像我想的那样运行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()

2 个答案:

答案 0 :(得分:4)

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

应该是

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

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

答案 1 :(得分:1)

您的引擎功能应该如此。

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