我和我的学生一起研究Python,并希望他们编写基于文本的游戏。我也在编写游戏,是一个相当新的编码器。我的问题是,当我使用def action1(run_leave):然后使用if和elif和else语句时,我的代码将无法运行。我怀疑我需要使用另一组代码,但不能理清哪些代码。一旦我看到它,我就可以说明它是如何工作的以及如何处理类似的其他代码集(action2等)。我把我的代码放在下面。任何帮助是极大的赞赏。第一个打印命令工作正常,我认为设置好心情 - 但是一旦我跑去或离开我就会出错。
编辑:我添加了错误消息: Traceback(最近一次调用最后一次): 文件“”,第1行,in 跑 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.")
答案 0 :(得分:2)
您的代码在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.
>>>
答案 1 :(得分:0)
在代码中,如果块错误,则为您发布缩进;您需要所有if
,elif
和else
排队(这是因为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.")