1

I am attempting a practice task I found in an old programming book to increase my knowledge of classes in Python. The task is to create a program which allows a user to set up a series of tests for a school. Each test must contain no more than 10 questions. The task stated that the best way to do this was to use containment, and have the class 'Question' inside the class 'Test'

Basically, I should set up a class called Test which dewfines the basics of the whole test, and then a class called Quesion which sets up the question and passes it back to Test to be included in the array there. I'm having 2 major problems. Firstly, how do I get the setQuestion object in the Question class to pass data in to the Question array in the Test class. Secondly, how do I have the setQuestion object iterate the variable numberofQuestions since that's contained in the Test Class.

Here is the code. Not sure it's clear from the formatting but the Question class is inside the Test class:

class Test(object):
    def __init__(self):
        self.__testID = 0
        self.__maxMarks = 0
        self.__questions = []
        self.__numberofQuestions = 0
        self.__level = ""
        self.__dateSet = ""


class Question(object):
    def __init__(self):
        self.__questionID = 0
        self.__questionText = ""
        self.__answer = ""
        self.__marks = 0
        self.__topic = ""

    def setQuestion(self, questionID, questionText, answer, marks, topic):
        self.__numberofQuestions = self.__numberofQuestions + 1
        self.__questionID = self.__questionID
        self.__questionText = self.__questionText
        self.__answer = self.__answer
        self.__marks = self.__marks
        self.__topic = self.__topic
6
  • I'm not sure what you are trying to do with self.__questionID = self.__questionID, but that does nothing. Commented Dec 17, 2016 at 7:35
  • Why do you define the Question class inside the Test class. Doesn't make any sense for me... Commented Dec 17, 2016 at 7:38
  • 1. Why are using those double underscores on all your attributes? That naming scheme is used to invoke name mangling, as explained in the tutorial, but you don't really need that here, so it just makes the code more complicated than it needs to be. Commented Dec 17, 2016 at 8:03
  • 2. You can define Question inside Test but there's little benefit from doing so, and (once again) it just adds unnecessary complexity to the code. As Wikipedia says, containment means using one class inside another, that doesn't imply that the contained class has to be defined inside the other class, it just has to be accessible to it. Commented Dec 17, 2016 at 8:03
  • The underscores were recommended in the book I'm reading. I will look more closely at them. Commented Dec 17, 2016 at 11:00

1 Answer 1

1

This is how I would do that:

class Test(object):
       def __init__(self,id,marks):
             self.__testID = id
             self.__maxMarks = marks
             self.__questions = []
             self.__numberofQuestions = 0
             self.__level = ""
             self.__dateSet = ""

      def setQuestion(self,question):
             self.__numberofQuestions += 1
             self.__questions.append(question)

class Question(object):
      def __init__(self,id,text,answer,marks,topic):
                  self.__questionID = id
                  self.__questionText = text
                  self.__answer = answer
                  self.__marks = marks
                  self.__topic = topic

Now you can put question objects into the __question array of Test like that:

if __name__ == "__main__":
       test = Test(1,100)
       test.setQuestion(Question(1,"Text","Answer",50,"Topic"))
Sign up to request clarification or add additional context in comments.

1 Comment

you could either use constructor or a setter method to pass your objects depending on situation.

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.