gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:19 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com