繁体   English   中英

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

[英]What is wrong with my simple code here?

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

这个

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]()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM