【问题标题】:Making a loop based on user input, then saving the result for a MUD in Python根据用户输入进行循环,然后将结果保存为 Python 中的 MUD
【发布时间】:2012-06-07 01:29:13
【问题描述】:

我正在尝试做两件事:

  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()

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

【问题讨论】:

  • 请注意,恶意用户可能会调用 blackdoor 函数,直到您的堆栈溢出,从而可能允许任意代码插入。

标签: python loops input mud


【解决方案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

【讨论】:

  • 我在尝试阿拉丁的代码时尝试了这个,由于某种原因,它必须在循环退出之前输入两次值。知道为什么吗?
  • 从您的设计来看,您似乎别无选择。您必须输入“错误”选择才能进入递归调用,然后输入“正确”选择才能退出。我的建议是重写它并删除递归。
【解决方案2】:

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

我会采用迭代解决方案,如下所示:

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()

【讨论】:

    【解决方案3】:

    我假设您在整个游戏中都有许多这样的决策点,而您在尝试将它们嵌入函数时会迷失方向。如果是这种情况,那么您应该使用简单的Finite State Machine (FSM) 进行探索。

    PythonWiki 上存在 FSM 实现列表。

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

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

    【讨论】:

    • 我以前读过这些,但我真的像 3 天前一样开始使用 python 编程。大声笑
    • 是的,这可能令人生畏。在您尝试了这些功能一段时间后,请记住这个建议。当您更有信心时,您可以尝试简单的教程或游戏编程入门书籍,以了解有关 FSM 的更多信息。 +1 欢迎使用 SO 和 Python。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多