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

标签 python python-2.7

您好,我在编写这个简单的程序时遇到了问题。我刚开始使用 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()

最佳答案

这个

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

应该是

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

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

关于python - 我这里的简单代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16088099/

相关文章:

Python 语法糖 : function arg aliases

python - 阻止值在每次字典迭代中递增

python - 使用 beautifulsoup 获取跨度标题

python - 使用列表理解和两段不同的代码写入文件

python - 接受 Python 中的 COM 错误

python LightGBM 文本分类与 Tfidf

python - 如何将情感分类器应用于数据框

python - 如何使用 python 脚本安装 msi?

python - 为什么这些 Python 代码的执行方式如此不同

python - 如何对同一个字典值同时使用键和索引?