1

I have a class called Question as per below

class Question:
    q_count = 0
    def __init__(self, s_id, q_id, question):
        print("q count is ", Question.q_count)       
        self._s_id = s_id
        self._question_text = question
        self._answers = []
        self._q_id = Question.q_count

    def get_answers(self):
        return self._answers

    def set_answers(self,answers):
        self._answers = answers

    def add_answer(self, Answer()):
        self._answers.append(Answer())

and I want it to have a list called _answers which is a list of Answer objects. where Answer is another type of class i have created.

class Answer:
    def __init__(self, q_id, a_id):
        self._q_id = q_id
        self._a_id = a_id



# Subclass for multiple choice
class MC_Answer(Answer):
    def __init__(self, q_id, a_id, answer_text):
        Answer.__init__(self, q_id, a_id)
        self._answer_text = answer_text

    def get_answer_text(self):
        return self._answer_text

    def set_answer_text(self, a_id):
        self._a_id = a_id;

In another part of my code, I am creating an instance of an MC_Answer object. I also have an instance of a question object. How do I append this onto its answers list??

        answer1 = MC_Answer(idQ, 1, answer_text)
        new_q.add_answer(answer1)
        write_to_file(alist1, "answers.csv")

this is wrong but if i don't have answer1 as a parameter, how does Python know to add that particular instance of an answer?

1 Answer 1

1

Your add answer class never uses the parameter, instead you make a new instance of the Answer class and append that to the list, instead you need to modify it to give it a parameter and then use it correctly

def add_answer(self, Answer()):
    self._answers.append(Answer())

should be

def add_answer(self, answer):
    self._answers.append(answer)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.