做一个thing类怎么样?在class thing:
def __init__(self, type = ''):
self.type = type
self.flight = (self.type in ['bird'])
self.swim = (self.type in ['fish', 'mammal'])
现在,选择一个随机的“东西”很简单:
^{pr2}$
所以你在做选择题?在
也许这会有用:class Question:
def __init__(self, question = '', choices = [], correct = None, answer = None):
self.question = question
self.choices = choices
self.correct = correct
def answer(self, answer):
self.answer = answer
def grade(self):
return self.answer == self.correct
class Test:
def __init__(self, questions):
self.questions = questions
def result(self):
return sum([question.grade() for question in self.questions])
def total(self):
return len(self.questions)
def percentage(self):
return 100.0 * float(self.result()) / float(self.total())
所以抽样测试应该是这样的:questions = [Question('What is 0 + 0?', [0, 1, 2, 3], 0),
Question('What is 1 + 1?', [0, 1, 2, 3], 2)]
test = Test(questions)
test.questions[0].answer(3) # Answers with the fourth item in answers, not three.
test.questions[1].answer(2)
print test.percentage()
# Prints 50.0