Carcanken Carcanken - 6 days ago 7
Python Question

Finding class attribute from list of initialized objects

I'm working on an assignment that involves creating a text adventure with a branching story line.

It works like this:

Level 1 has 1 prompt and 3 choices
Level 2 has 3 prompts and 9 choices
Level 3 has 9 prompts and 27 choices
Level 4 has 27 prompts and 81 choices
Each choice taken leads to a new and unique prompt on the next level with 3 different choices.

I have a basic class set up like this:

class Level:
def __init__(self,level_num, level_prompt):
self.level_num = level_num
self.level_prompt = level_prompt
self.choices = []

def add_choices(self, choices, next_branch):
self.choices.append(choices)
self.next_branch = next_branch

def print_situation(self):
print("Level " + str(self.level_num))
print(self.level_prompt)
print("[A] " + self.choices[0])
print("[B] " + self.choices[1])
print("[C] " + self.choices[2])

def get_next_prompt(self, letter_choice): # this will look at choice and find the prompt associated with it, for example level1 choice A SHOULD return level2_A
if letter_choice == 'A':
return self.choices[0].next_branch
if letter_choice == 'B':
return self.choices[1].next_branch
if letter_choice == 'C':
return self.choices[2].next_branch


For the purpose of explaining the flow of the program, here's a sample of the initialized objects.

# prompts
level1 = Level(1, 'PROMPT HERE')

level2_A = Level(2, 'PROMPT HERE')
level2_B = Level(2, 'PROMPT HERE')
level2_C = Level(2, 'PROMPT HERE')

level3_A1 = Level(3, 'PROMPT HERE')
level3_A2 = Level(3, 'PROMPT HERE')
level3_A3 = Level(3, 'PROMPT HERE')
level3_B1 = Level(3, 'PROMPT HERE')
level3_B2 = Level(3, 'PROMPT HERE')
level3_B3 = Level(3, 'PROMPT HERE')
level3_C1 = Level(3, 'PROMPT HERE')
level3_C2 = Level(3, 'PROMPT HERE')
level3_C3 = Level(3, 'PROMPT HERE')

# choices
level1.add_choices('CHOICE A', level2_A)
level1.add_choices('CHOICE B', level2_B)
level1.add_choices('CHOICE C', level2_C)

level2_A.add_choices('CHOICE A', level3_A1)
level2_A.add_choices('CHOICE B', level3_A2)
level2_A.add_choices('CHOICE C', level3_A3)
level2_B.add_choices('CHOICE A', level3_B1)
level2_B.add_choices('CHOICE B', level3_B2)
level2_B.add_choices('CHOICE C', level3_B3)
level2_C.add_choices('CHOICE A', level3_C1)
level2_C.add_choices('CHOICE B', level3_C2)
level2_C.add_choices('CHOICE C', level3_C3)


The error in my program comes from the get_next_prompt method of my class. My intention is to be able to call it like this:

level1.get_next_prompt('A')


and have it return the value: level2_A. This isn't simply adding _A to the end of the level name, but it should look at the next_branch argument of the choice and return that.

Let me know if something isn't entirely clear.

Thanks!

Answer
def add_choices(self, choices, next_branch):
        self.choices.append(choices)
        self.next_branch = next_branch

self.choices is an array of strings: ['CHOICE A','CHOICE B', 'CHOICE C'], which don't have any next_branch attribute.

You're not storing the next_branch with each choice, you're storing it in the base level and just overwriting it every time you add a new choice.

I'd probably change it to:

def add_choices(self, choices, next_branch):
        self.choices.append((choices, next_branch))

then your choice text is at self.choices[0][0], self.choices[1][0], self.choices[2][0] and the next branches are stored at self.choices[0][1], self.choices[1][1], self.choices[2][1]

A better option would be to change the level structure so you initialize it as:

level1 = Level(1, 'CHOICE TO GET HERE', 'PROMPT HERE')

stored as

self.choice
self.prompt

Then you can do

def add_choices(self, next_branch):
    self.choices.append(next_branch)

and get the information from:

print self.choices[0].choice

or to get the level

return self.choices[0]