基于文本的gam编码循环

2024-06-02 07:50:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在和我的学生一起研究Python,希望他们编写一个基于文本的游戏。我也在编写游戏,作为一个相当新的程序员。我的问题是,当我使用def action1(run\u leave):然后使用if和elif以及else语句时,我的代码不会运行。我怀疑我需要使用另一套代码,但无法分辨出是哪一套。一旦我看到它,我就可以知道它是如何工作的,以及如何处理其他类似的代码集(action2,等等)。我把代码放在下面了。非常感谢您的帮助。第一个print命令运行得很好,我认为可以很好地设置情绪-但是一旦我开始运行或离开,就会出现错误。你知道吗

编辑:我添加了错误消息: 回溯(最近一次呼叫): 文件“”,第1行,在 运行 NameError:未定义名称“run”

提前谢谢!你知道吗

布鲁斯

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.")

Tags: andofthetorun代码inyou
2条回答

在代码中,如果块是错误的,则为您发布缩进;您需要所有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.")

您的代码在python3.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.
>>> 

相关问题 更多 >