我一直在学习python的最近几个月,现在我正在尝试创建一个文本冒险游戏。程序/设置的要求如下:Python文本冒险存储数据的麻烦
根据用户的选择创建总共4个级别的文本冒险游戏。
根据您之前所做的选择,每个级别都必须有三个不同的选择。
例如,等级1有3个选择,每个等级将你带到等级2.根据你在等级1中的选择,你现在有3个选择,导致等级1有3个选择,等级2具有9,3级有27级,4级有81级。
在我的代码中,我有一个用于创建提示和选择的基本设置,但我很努力地找到一种方法来将每个特定的提示与三个选项相关联。
这是到目前为止我的代码:
# every prompt has an associated 3 choices with it, and every
choice made has a prompt with three more choices to be made
associated with it.
def print_segment(prompt, choice1, choice2, choice3):
print(prompt)
print("[A] " + choice1)
print("[B] " + choice2)
print("[C] " + choice3)
answer = input().lower()
if answer == "A": # if answer is A, print the next prompt associated with A
if answer == "B": # if answer is B, print the next prompt associated with B
if answer == "C": # if answer is C, print the next prompt associated with C
# level 1 choices
level1_choice1 = "Go to the scene of the most recent murder."
level1_choice2 = "Go to the scene of the first murder."
level1_choice3 = "Wait a few hours and see how the situation develops."
# level 1 prompts
level1_prompt1 = '''You are a murder investigator and you are tasked on the case of a mysterious string of killings
that have happened in the past few weeks. How do you start?'''
# level 2 prompts
level2_prompt1 = "You arrive at the scene of the most recent murder. What would you like to do first?"
level2_prompt2 = "You arrive at the scene of the first murder. What would you like to do first?"
level3_prompt3 = "You receive a letter from an unknown source saying that you should meet them at a specific location. What do you do?"
print_segment(level1_prompt1, level1_choice1, level1_choice2, level1_choice3)
我想尽可能彻底与这个解释这样的事情就不会感到困惑,但我在我的print_segment大多寻求帮助功能。评论描述了我正面临的问题,我想知道如何存储所有提示和选择数据。最好是创建一个带有三个选择的提示词典?如果我这样做,我将如何去关联level1_choice1与level2_prompt1?
让我知道如果有什么不清楚。
非常感谢!
虽然它可能不是最好的代表,你可以*在这里使用字典。最外面的键将是第一级的选择,其余的可能性的字典,作为值,给出该选择。这将是一个结构递归的例子。 – BlackVegetable