[Tutor] results not quite 100 percent yet
Kent Johnson
kent37 at tds.net
Wed Jan 30 18:46:48 CET 2008
bhaaluu wrote:
> # distribute positive numbers 10 to 109
> # place in last element of 4 random lists
> # nothing is placed in list 6 or 11
> cnt=0
> while cnt <= 3:
> a = range(1,20)
> room = random.choice(a)
room = random.randint(1, 19) is simpler.
> if room != 6 and room != 11 and travelTable[room-1][6] == 0:
> b = range(10,110)
> treasure = random.choice(b)
Use randint() here too.
> travelTable[room-1][6] = treasure
> else:
> cnt -= 1
> cnt += 1
This use of cnt is a bit strange. Why not increment only in the
successful 'if' and get rid of the 'else' entirely?
Rather than repeating the loop until you get three distinct, valid
random numbers, you could do something like this:
# Change 7 rooms, not room 6 or 11
changeableRooms = range(1, 20)
changeableRooms.remove(6)
changeableRooms.remove(11)
roomsToChange = random.sample(changeableRooms, 7)
# First three get something good
for room in roomsToChange[:3]:
travelTable[room-1][6] = random.randint(10, 119)
# Last four get something bad
for i, room in enumerate(roomsToChange[3:]):
travelTable[room-1][6] = -i-1
> print " 1:", travelTable[0][6]
etc - use a loop and string formatting:
for i, room in enumerate(travelTable):
print ' %s: %s' % (i+1, room[6])
Finally, you might consider putting a dummy entry at travelTable[0], or
number the rooms from 0, so you don't have to adjust the indices all the
time.
Kent
More information about the Tutor
mailing list