我正在尝试做两件事:
在 python 中创建一个循环,为用户提供 4 个可能选项的提示;输入“1”、“2”、“3”或其他任何内容。如果用户选择 1、2 或 3,则会显示文本。如果用户输入任何其他内容,他们会看到文本,并再次提示。如此重复,直到他们输入 1、2 或 3。
然后我想从用户那里获取输入以在该循环之外使用并继续游戏。
到目前为止我的解决方案:在我发布我的代码之前,我将描述它,我基本上已经将我想要的所有代码放在一个没有参数的函数中的循环中。然后我在 else 语句中调用该函数。
代码在做什么:代码按照我想要的方式循环,但我不知道如何根据用户输入的内容“跳出”循环以继续。我知道它必须是一个回报,但我想我不知道把它放在哪里。
我尝试过的:发布调用我输入的函数:
if blackdoor(decision) == "1":
并从那里继续,但这不起作用。
编码:
def blackdoor():
print """After having recently died you awake to find yourself standing in an all white room with a black door that seems to go into the sky forever. What do you do?\n1.Touch the door.\n2.Shout at the door.\n3.Stare at the door."""
decision = raw_input("> ")
if decision == "1":
print "You touch the door. It is as cold as ice. You can feel a vibration pulsing from the door through your body.\n"
elif decision == "2":
print "You shout 'Hello?! Is anybody there?!' at the door. But nothing responds. Your voice echoes off in the distance.\n"
elif decision == "3":
print "You stare at the door intensely. You envision it opening when all of a sudden you get the feeling of something staring back at you.\n"
else:
print "You don't follow instructions very well, do you?\n"
blackdoor()
return decision
blackdoor()
我如何才能将输入用于决策以将其用作保持游戏继续进行的条件?