为基于文本的游戏编码循环



我正在和我的学生一起开发Python,希望他们编写一个基于文本的游戏。我也在编码游戏,作为一个相当新的编码器。我的问题是,当我使用defaction1(run_leave):然后使用if和elif以及else语句时,我的代码将无法运行。我怀疑我需要使用另一组代码,但无法确定是哪一组。一旦我看到它,我就可以了解它是如何工作的,以及如何处理其他类似的代码集(action2等)。我已经把我的代码放在下面了。非常感谢您的帮助。第一个打印命令运行良好,我认为设置气氛非常好,但一旦我开始运行或离开,就会出现错误。

编辑:我添加了错误消息:追踪(最近一次通话):文件",第1行,在跑名称错误:未定义名称"run"

提前感谢!

Bruce

print("""After a long and dangerous journey, you stand at your destination - the Pyramid of Osiris, the God of the Dead. It has been a long strange journey, almost as if
fate did not want you to arrive. But arrive you have!""")
print(" ")
print("""Now you stand in from of the Pyramid as a work crew led by Salim Al-Salaam
clears the last of the debris away from a magnificent set of sealed double
doors.You can feel the tension in the air as the last basket of rocks are
carried away from the entrance.""")
print(" ")
print("""As you gaze up at the steps and entrance, the briliance of the sun vanishes as
dark clouds cover it, and in the distance you see bright flashes of lightning,
followed by the rolling boom of thunder. A major storm is coming in.""")
print(" ")
print("""What do you choose to do?
Run for the entrance. (run)
Go back to your car and leave, because this is a sign you were never meant to
explore the pyramid. (leave)""")
print(" ")
def action1(run_leave):
    if run_leave == "run":
        print("""As you sprint up the stairs, you smell the ozone in the air getting
stronger. Suddenly, a lightning bolt flashes out of nowhere and strikes the stairs where you were just moments before, making your hair stand on end. Perhaps this wasn't such a good idea...""")
    elif run_leave == "leave":
        print("""In the car driving away, you look in the rear view mirror to see a strike of lightning hit the top of the Pyramid of Osiris, which begins to glow softly. As you turn around to watch the road ahead of you, a strange peace settles over your soul, and you realize you made the right choice.""")
    else:
        print("Your answer did not make sense. Leaving the game.")

您的代码在Python 3.5.0中运行良好。

请检查您是否正确调用了该函数。它应该被称为action1('run')而不是action1(run)

After a long and dangerous journey, you stand at your destination - the Pyramid of Osiris, the God of the Dead. It has been a long strange journey, almost as if
fate did not want you to arrive. But arrive you have!
Now you stand in from of the Pyramid as a work crew led by Salim Al-Salaam
clears the last of the debris away from a magnificent set of sealed double
doors.You can feel the tension in the air as the last basket of rocks are
carried away from the entrance.
As you gaze up at the steps and entrance, the briliance of the sun vanishes as
dark clouds cover it, and in the distance you see bright flashes of lightning,
followed by the rolling boom of thunder. A major storm is coming in.
What do you choose to do?
Run for the entrance. (run)
Go back to your car and leave, because this is a sign you were never meant to
explore the pyramid. (leave)
>>> action1('run')
As you sprint up the stairs, you smell the ozone in the air getting
stronger. Suddenly, a lightning bolt flashes out of nowhere and strikes the stairs where you were just moments before, making your hair stand on end. Perhaps this wasn't such a good idea...
>>> action1('leave')
In the car driving away, you look in the rear view mirror to see a strike of lightning hit the top of the Pyramid of Osiris, which begins to glow softly. As you turn around to watch the road ahead of you, a strange peace settles over your soul, and you realize you made the right choice.
>>> action('shit')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    action('shit')
NameError: name 'action' is not defined
>>> action1('shit')
Your answer did not make sense. Leaving the game.
>>> 

在代码中,如果块是错误的,您会为您发布缩进;您需要所有的ifelifelse排成一行(这是因为Python具有有意义的空白,不像javascript或C等其他语言)

    if run_leave == "run":
        print("""As you sprint up the stairs, you smell the ozone in the air getting
stronger. Suddenly, a lightning bolt flashes out of nowhere and strikes the stairs where you were just moments before, making your hair stand on end. Perhaps this wasn't such a good idea...""")
    elif run_leave == "leave":
        print("""In the car driving away, you look in the rear view mirror to see a strike of lightning hit the top of the Pyramid of Osiris, which begins to glow softly. As you turn around to watch the road ahead of you, a strange peace settles over your soul, and you realize you made the right choice.""")
    else:
        print("Your answer did not make sense. Leaving the game.")

最新更新