Cannon Sloan Cannon Sloan - 25 days ago 7
Python Question

Python text adventure trouble with storing data

I have been learning python for the last few months and right now I am trying to create a text adventure game. The requirements for the program/setup are as follows:

Create a text adventure game with a total of 4 levels based on choices made by the user.

Each level must have three different choices based on choices you have made before.

So for example, level 1 has 3 choices, with each taking you to level 2. Based on the choice you made in level 1, you will now have 3 more choices resulting in level 1 having 3 choices, level 2 having 9, level 3 having 27, and level 4 having 81.

In my code I have a basic setup for creating the prompts and choices, but I'm struggling to find a way to associate each specific prompt with three choices.

Here is my code so far:

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


I'm trying to be as thorough as possible with this explanation so things don't get confused, but I'm looking for help mostly in my print_segment function. The comments describe the issue I'm facing and I'm wondering how to store all of the prompt and choice data. Would it be best to create a dictionary of prompts with three choices? If I do that, how would I go about associating level1_choice1 with level2_prompt1?

Let me know if something isn't clear.

Thanks a lot!

Answer

Is this the structure you're after?

choices = {
    1 : { 'prompt' : {
            'prompt' : 'Level 1 prompt',
            'A' : 'Choice A',
            'B' : 'Choice B',
            'C' : 'Choice C' },
        },
    2 : { 'promptA' : {
            'prompt' : 'Level 2 prompt A',
            'A' : 'A Choice A',
            'B' : 'A Choice B',
            'C' : 'A Choice C' },
          'promptB' : {
            'prompt' : 'Level 2 prompt B',
            'A' : 'B Choice A',
            'B' : 'B Choice B',
            'C' : 'B Choice C' },
          'promptC' : {
            'prompt' : 'Level 2 prompt C',
            'A' : 'C Choice A',
            'B' : 'C Choice B',
            'C' : 'C Choice C' },
        },
    3 : { 'promptA' : {
            'prompt' : 'Level 3 prompt A',
            'A' : 'AChoice A',
            'B' : 'A Choice B',
            'C' : 'A Choice C' },
          'promptB' : {
            'prompt' : 'Level 3 prompt B',
            'A' : 'B Choice A',
            'B' : 'B Choice B',
            'C' : 'B Choice C' },
          'promptC' : {
            'prompt' : 'Level 3 prompt C',
            'A' : 'C Choice A',
            'B' : 'C Choice B',
            'C' : 'C Choice C' },
        }
    }

def print_segment(level, prev_choice = ''):
    d = choices.get(level).get('prompt' + prev_choice)
    print(d.get('prompt'))
    for c in 'ABC':
        print("[{}] {}".format(c, d.get(c)))

# Output
>>> print_segment(1)
Level 1 prompt
[A] Choice A
[B] Choice B
[C] Choice C
>>> print_segment(2, 'A')
Level 2 prompt A
[A] A Choice A
[B] A Choice B
[C] A Choice C
>>> print_segment(3, 'B')
Level 3 prompt B
[A] B Choice A
[B] B Choice B
[C] B Choice C