gpt4 book ai didi

python - 根据用户输入创建循环,然后将结果保存为 Python 中的 MUD

转载 作者:行者123 更新时间:2023-12-01 06:00:06 25 4
gpt4 key购买 nike

我正在尝试做两件事:

  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(),但没有返回调用结果。您需要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

关于python - 根据用户输入创建循环,然后将结果保存为 Python 中的 MUD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10856499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com