1

我正在尝试做两件事:

  1. 在 python 中创建一个循环,为用户提供 4 个可能选项的提示;输入“1”、“2”、“3”或其他任何内容。如果用户选择 1、2 或 3,则会显示文本。如果用户输入任何其他内容,他们会看到文本,并再次提示。如此重复,直到他们输入 1、2 或 3。

  2. 然后我想从用户那里获取输入以在该循环之外使用并继续游戏。

到目前为止我的解决方案:在我发布我的代码之前,我将描述它,我基本上已经将我想要的所有代码放在一个没有参数的函数中的循环中。然后我在 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()

我如何才能将输入用于决策以将其用作保持游戏继续进行的条件?

4

3 回答 3

1

根据我对您的问题的理解,您的代码看起来不错,但是,如果您没有将递归限制设置为很大,它可能会由于多次递归而耗尽堆栈。

我会寻求一个迭代解决方案,如下所示:

def blackdoor():
    end_condition = False
    while not end_condition:
        end_condition = True
        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"
            end_condition = False
    return decision


decision = blackdoor()
于 2012-06-01T20:03:30.520 回答
1

您正在递归调用blackdoor(),但您没有返回调用的结果。您需要return blackdoor()将结果返回给调用者。

一个简单的改变就可以了;

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"
    return blackdoor()
return decision
于 2012-06-01T19:52:34.023 回答
0

我假设你在整个游戏中都有很多这样的决策点,你会在试图将它们嵌入函数时迷失方向。如果是这种情况,那么您应该探索使用简单的有限状态机(FSM)。

PythonWiki上有一个 FSM 实现列表。

如果你只有一两个动作,使用 FSM 似乎有点过头了,但你会发现,即使是一个简单的冒险游戏,如果没有一个动作的帮助,编写一个简单的冒险游戏也是不必要的困难。

更新我在Python 和 FSM上找到了一个有用的答案,以供将来参考。

于 2012-06-01T20:28:36.030 回答