# L-19 MCS 260 Wed 10 Oct 2007 : questions # # The module questions generates questions, # shuffles the answers, evaluates the choice, # and keeps the tally of right and wrongs. import quand def generate(): "returns a multiple choice question\ \nas a tuple (q,L), where q is a number,\ \nand L is a list of numbers with answers" import random q = random.randint(1,len(quand.questions)) L = range(1,len(quand.answers[q])+1) return (q,L) def shuffle(L): "shuffles the order of the answers\ \non return is a permutation of the list L" import random n = len(L) b = L s = [] while n > 0: i = random.randint(0,n-1) s.append(b.pop(i)) n = n - 1 return s def evaluate(L,c): "evaluates choice c to True or False\ \nwhere L is a list of answers" return (L[c-1] == 1) def update_tally(t,b): "updates tally of True or False answers\ \nwhere t is a list of two elements and\ \nb is True or False, returns the updated tally" nt = t if b: nt[1] += 1 else: nt[0] += 1 return nt