我一直在學習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