[Tutor] Random module help

Norman Khine norman at khine.net
Sat Oct 6 09:04:00 CEST 2007


Hello,
I have an 'Exam' module which selects new random set of questions and 
displays it, well this is the idea n;). Here is what I have so far:

 >>> import random
 >>> n_question = 5
 >>> q = {1:'1q', 2:'2q', 3:'3q', 4:'4q', 5:'5q', 6:'6q', 7:'7q', 
8:'8q', 9:'9q', 0:'0q'}
 >>> if len(q) > n_question:
...     q_keys = random.sample(q.keys(), n_question)
... else:
...     q_keys = q.keys()
...
 >>> q_keys.sort()
 >>> a = [q[x] for x in q_keys]
 >>> a
['1q', '4q', '5q', '7q', '8q']
 >>> a
['1q', '4q', '5q', '7q', '8q']


This only returns the same questions, what am I doing wrong? How do I 
return a different set?

I would like to have a group of similar questions and then select a 
random set out of this to make up my final namespace (a), thus avoiding 
similar questions being selected from the pool of questions.

Is there a more elegant solution which will pull a random selection of 
questions?

Cheers

-- 
Norman

-- 
Norman Khine
7 rue Ponscarme, Paris, 75013, France
tel +33 870 628 934 fax +33 142 724 4437

%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) 
for c in ",adym,*)&uzq^zqf" ] )



More information about the Tutor mailing list