[Tutor] results not quite 100 percent yet

bhaaluu bhaaluu at gmail.com
Wed Jan 30 18:19:05 CET 2008


On Jan 30, 2008 3:35 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> In addition to Kents comments about dictionaruy
> access I think there may be another problem in
> your logic.
>
>
> "bhaaluu" <bhaaluu at gmail.com> wrote
>
> > The first loop is supposed to populate G with
> > a random range of 4 integers 10 to 109
> > in random keys 1-19 that have a zero (except keY 6 and  keY 11)
> > So keY 6 and keY 11 should both have a zero in G after the
> > four integers have been sown.
> >
> >    if keY == 6 or keY == 11 or tablE.values()[keY-1][6] != 0:
> >        tablE.values()[5][6] = 0
> >        tablE.values()[10][6] = 0
> >        cnt -= 1
> >        keY = random.choice(a)
>
> This detects any of the exception cases so a simple else
> clause should be sufficient for the others. However if you
> really want an explicit check...
>
> >    if keY != 6 or keY != 11 or table.values()[keY-1][6] == 0:
>
> This test should use 'and' rather than 'or' since you want
> all of the conditions to be true, not just one of them.
> But since the failing condituions should all have been
> caught above simply using else here would do what
> I think you want.
>
>
> >        b = range(10,110) # 10 to 109
> >        integer = random.choice(b)
> >        tablE.values()[keY-1][6] = integer
> >        cnt += 1
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld

Thank you Alan and Kent.
Your suggestions caused me to completely start
from scratch. It also looks like the "nested sequence"
will be easier to use in the long run (and I'm into this
for the long run!). 8^D

This is working properly now on my
GNU/Linux system running Python 2.4.3:

#!/usr/bin/python
# 2008-01-30

import random

# setup environment
#             N S E W U D T
travelTable=[[0,2,0,0,0,0,0],    # ROOM 1
             [1,3,3,0,0,0,0],    # ROOM 2
             [2,0,5,2,0,0,0],    # ROOM 3
             [0,5,0,0,0,0,0],    # ROOM 4
             [4,0,0,3,5,13,0],   # ROOM 5
             [0,0,1,0,0,0,0],    # ROOM 6
             [0,8,0,0,0,0,0],    # ROOM 7
             [7,0,0,0,0,0,0],    # ROOM 8
             [0,9,0,0,0,8,0],    # ROOM 9
             [8,0,11,0,0,0,0],   # ROOM 10
             [0,0,10,0,0,0,0],   # ROOM 11
             [0,0,0,13,0,0,0],   # ROOM 12
             [0,0,12,0,5,0,0],   # ROOM 13
             [0,15,17,0,0,0,0],  # ROOM 14
             [14,0,0,0,0,5,0],   # ROOM 15
             [17,0,19,0,0,0,0],  # ROOM 16
             [18,16,0,14,0,0,0], # ROOM 17
             [0,17,0,0,0,0,0],   # ROOM 18
             [9,0,0,16,0,0,0]]   # ROOM 19

# 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)
    if room != 6 and room != 11 and travelTable[room-1][6] == 0:
        b = range(10,110)
        treasure = random.choice(b)
        travelTable[room-1][6] = treasure
    else:
        cnt -= 1
    cnt += 1

# distribute negtive numbers -4 to -1
# place in last element of 4 random lists
# nothing is placed in list 6 or 11
cnt=4
while cnt > 0:
    a = range(1,20)
    room = random.choice(a)
    if room != 6 and room != 11 and travelTable[room-1][6] == 0:
        travelTable[room-1][6] = -cnt
    else:
        cnt += 1
    cnt -= 1

# put positive numbers in the last element
# of two specific lists overwriting any
# number that exists
a = range(1,99)
travelTable[3][6]= 100 + random.choice(a)
travelTable[15][6]= 100 + random.choice(a)

print " 1:", travelTable[0][6]
print " 2:", travelTable[1][6]
print " 3:", travelTable[2][6]
print " 4:", travelTable[3][6]
print " 5:", travelTable[4][6]
print " 6:", travelTable[5][6]
print " 7:", travelTable[6][6]
print " 8:", travelTable[7][6]
print " 9:", travelTable[8][6]
print "10:", travelTable[9][6]
print "11:", travelTable[10][6]
print "12:", travelTable[11][6]
print "13:", travelTable[12][6]
print "14:", travelTable[13][6]
print "15:", travelTable[14][6]
print "16:", travelTable[15][6]
print "17:", travelTable[16][6]
print "18:", travelTable[17][6]
print "19:", travelTable[18][6]
# end of program

-- 
b h a a l u u at g m a i l dot c o m
"You assist an evil system most effectively by obeying its
orders and decrees. An evil system never deserves such
allegiance.  Allegiance to it means partaking of the evil.
A good person will resist an evil system with his or her
whole soul." [Mahatma Gandhi]


More information about the Tutor mailing list