IN PYTHON PLEASE Create a sub class MultiChoiceQuestion to the question hierarch
ID: 3839343 • Letter: I
Question
IN PYTHON PLEASE
Create a sub class MultiChoiceQuestion to the question hierarchy of Section 10.1 that allows multiple correct choices. The respondent should provide all correct choices, separated by spaces. Provide instructions in the question text.
Below is the program that runs the class MultiChoiceQuestion.py.
# This program shows a simple quiz with one question.
# Create the question and expected answer.
q = MultiChoiceQuestion()
q.setText("Of Apple, Tomato, Carrot, Cucumber and Celery, list all that are fruit.")
q.setAnswer("Apple Tomato")
# Display the question and obtain user's response.
q.display()
response = input("Your answer: ")
print(q.checkAnswer(response))
Note: Responses of “Apple Tomato” or “Tomato Apple” are both correct. Demonstrate both.
Your code with comments
A screenshot of the execution
Explanation / Answer
#multichoice_question.py
class MultiChoiceQuestion:
def __init__(self):
self.question = ""
self.options = []
self.answers = []
def setText(self,text):
self.question = text
def setOptions(self,optionstext):
self.options = optionstext.split(" ")
def setAnswers(self,answertext):
self.answers = answertext.split(" ")
def display(self):
print "QUESTION : "+self.question
i =1
for opt in self.options:
print str(i)+". "+opt
def checkAnswer(self,ans):
words = ans.split(" ")
stat = False
for word in words:
#print word
stat = False
for corr in self.answers:
if(corr == word):
stat = True
break
if(stat == False):
break
if(stat == False):
return False
return True
def __str__(self):
return "Discription : "
def main():
q= MultiChoiceQuestion()
q.setText("list all that are fruit.")
q.setOptions("Apple Tomato Carrot Cucumber Celery")
q.setAnswers("Apple Tomato")
q.display();
response = raw_input("Your answer: ")
print "RESULT : "
print(q.checkAnswer(response))
main()