python随机分配代码_为列表中的随机元素分配变量?Python

做一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值