# L-22 MCS 260 Wed 3 Mar 2008 : 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 as a tuple (q,L), where q is a number, and 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 on 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 where L is a list of answers. """ return (L[c-1] == 1) def update_tally(t,b): """ Updates tally of True or False answers where t is a list of two elements and b is True or False, returns the updated tally. """ nt = t if b: nt[1] += 1 else: nt[0] += 1 return nt