[英]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. 级别1具有1个提示和3个选择级别2具有3个提示和9个选择级别3具有9个提示和27个选择级别4具有27个提示和81个选择采取的每个选择都会在下一级别上产生3种不同选择的新的唯一提示。
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. 我程序中的错误来自类的get_next_prompt方法。 My intention is to be able to call it like this:
我的意图是能够这样称呼它:
level1.get_next_prompt('A')
and have it return the value: level2_A. 并使其返回值: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.
这不仅是在级别名称的末尾添加_A,而且还应查看选择项的next_branch参数并将其返回。
Let me know if something isn't entirely clear. 让我知道是否有一些不清楚的事情。
Thanks! 谢谢!
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. self.choices
是一个字符串数组: ['CHOICE A','CHOICE B', 'CHOICE C']
,没有任何next_branch
属性。
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. 您没有将
next_branch
与每个选择一起存储, next_branch
将其存储在基本级别中,并且每次添加新选择时都将其覆盖。
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]
那么您选择的文本位于
self.choices[0][0], self.choices[1][0], self.choices[2][0]
,下一个分支存储在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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.