[Tutor] Set changing order of items?
Adam Cripps
kabads at gmail.com
Fri Jan 19 20:18:36 CET 2007
I'm adding strings to a Set to prevent duplicates. However, the
strings are meant to be in the correct order. When I add the string to
the Set, the order seems to change (and I don't seem to be able to
predict what order they are in).
The string is a simple addition question which should look like this
1) 20 + 3 = 23
I've kept the question ("1) 20 + ") separate from the answer ("23") so
that I can hide the answers later on.
The code that produces this is:
problem_set.add(str(count) +") "+ str(a)+ " + "+ str(b)+ " = ")
problem_set.add(str(a + b) + "\n")
However str(problem_set) returns something more like:
(['18\n', '2) 6 + 9 = ', '15\n', '1) 9 + 9 = '])
Is there a better way of organising this? Any suggestions gratefully
received. In writing this email, I've realised that already I run the
risk of having different problems (20 + 1 and 19 + 2) which have the
same answer (21) which won't be allowed in the set.
The whole code can be seen at[1].
TIA
Adam
--
http://www.monkeez.org
PGP key: 0x7111B833
[1]
def add (lowrange, hirange, i):
"""Returns a list of additions for i iterations."""
count = 1
sums = []
problem_set = set()
while len(problem_set)<=i*2:
#while count <=i:
a = randint(lowrange, hirange)
b = randint (lowrange, hirange)
problem_set.add(str(count) +") "+ str(a)+ " + "+ str(b)+ " = ")
problem_set.add(str(a + b) + "\n")
count = count + 1
#sums.append(sum)
print "len problem_set " + str(len(problem_set))
print "problem_set " + str(problem_set)
sums = list(problem_set)
print "len sums " + str(len(sums))
#sums1 = sums.reverse()
print "sums " + str(sums)
return sums
More information about the Tutor
mailing list