Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于文本的游戏循环编码_Python - Fatal编程技术网

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

Python 基于文本的游戏循环编码,python,Python,我正在和我的学生一起研究Python,希望他们编写一个基于文本的游戏。作为一名新的程序员,我也在编写游戏代码。我的问题是,当我使用def action1(run_leave):然后使用if和elif以及else语句时,我的代码将无法运行。我怀疑我需要使用另一组代码,但无法确定是哪一组。一旦我看到它,我就可以知道它是如何工作的,以及如何处理其他类似的代码集(action2,等等)。我已经把代码放在下面了。非常感谢您的帮助。第一个print命令工作得很好,我认为可以很好地调节情绪——但一旦我开始运行

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

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

提前谢谢

布鲁斯


在代码中,如果块是错误的,则为您发布缩进;您需要将所有的
if
elif
else
对齐(这是因为Python具有有意义的空格,不像javascript或C等其他语言)


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

请检查您是否正确调用了该函数。它应该被称为action1(“运行”),而不是action1(运行)

经过漫长而危险的旅程,你站在了你的目的地——死亡之神奥西里斯金字塔。这是一段漫长而奇怪的旅程,几乎就好像
命运不希望你到来。但是你到了!
现在你站在金字塔的另一端,作为一个由萨利姆·萨拉姆领导的工作小组
清除最后的碎片远离一套华丽的密封双
门。你可以感觉到最后一筐石头在晃动时空气中的紧张
从入口被带走。
当你抬头凝视台阶和入口时,阳光的光辉也随之消失
乌云覆盖着它,在远处你可以看到明亮的闪电,
接着是隆隆的雷声。一场大风暴即将来临。
你选择做什么?
向入口跑去。(运行)
回到你的车上,离开,因为这是一个你从未想过要离开的信号
探索金字塔。(离开)
>>>操作1(“运行”)
当你冲上楼梯时,你闻到空气中的臭氧越来越多
更强突然,一道闪电不知从哪里冒了出来,击中了你刚才所在的楼梯,让你毛骨悚然。也许这不是个好主意。。。
>>>行动1(“离开”)
在开走的车里,你从后视镜里看到一道闪电击中了奥西里斯金字塔的顶部,它开始发出柔和的光芒。当你转身看着前方的道路时,一种奇怪的平静笼罩着你的灵魂,你意识到你做出了正确的选择。
>>>行动(“狗屎”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
行动(“狗屎”)
NameError:未定义名称“操作”
>>>行动1(“狗屎”)
你的回答没有道理。离开比赛。
>>> 

谢谢maxymoo。我仔细检查了空格的缩进,没问题。我仍然收到以下消息:>>>>runTraceback(最近一次调用last):文件“”,运行名中的第1行错误:名称“run”未定义您如何调用
action1
?你要传递什么样的参数?发布交互式shell的文本,而不是它的屏幕截图。好的。很抱歉我是这里的新手,嗨,普里扬卡。谢谢你的回复。如果你能发布代码,让我看看它是如何格式化的,那就太好了。我是一个视觉型的人,所以视觉是成功的一半:-)我也在看一个用python制作基于文本的游戏的网站,但我担心它对我的学生来说可能太高级了。然而,这是相当循序渐进的。布鲁斯,嘿,布鲁斯。我没有添加任何新代码。我刚刚检查了你在问题中发布的代码。你的代码运行良好。当您运行代码时,它会在交互式shell中打开(我在上面发布了它的文本)。由于您没有在任何地方“调用”该函数,您可能需要像我在这里所做的那样,在交互式shell中手动调用该函数来检查它。您可以添加代码以从播放器获取输入,然后调用该函数,这样您就不必在运行程序后执行该操作。因此,您可以添加如下代码:text=input(),然后调用函数:action1(text)
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.")
    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.")
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.
>>>