[Tutor] results not quite 100 percent yet

bhaaluu bhaaluu at gmail.com
Wed Jan 30 15:07:30 CET 2008


On Jan 30, 2008 8:24 AM, Kent Johnson <kent37 at tds.net> wrote:
> bhaaluu wrote:
> > Now that you mention it, I do seem to remember that the order of
> > a list is indeterminate.
>
> No; the order of a dict is indeterminate, and consequently the order of
> lists derived from dicts with keys(), values(), etc. is indeterminate.

The order of the dictionary is indeterminate. But the key is always
attached to the value, and in my case, the value is a list, so

print tablE.keys() #prints all the keys [in an ordered list, 1-19]

print tablE.keys()[5] #prints the key, 6

print tablE.values() #prints a list of [all [the lists]]

print tablE.values()[5] #prints only the [list for key 6]

print tablE.values()[5][2] #prints the third element: 1

So what you're saying here is that while it might work okay on
my system, that this may not work the same way on another
system?

>
> The order of a list is determined by how you create it.

Yes, this is how it works in my list test, and this is what my Python
books say. (Although I may have forgotten that on a conscious level,
I am now reminded of it.) 8^D

> >
> > What kind of Python array structure would you use if you need to
> > access each element in the exact order it appears in the table?
>
> A list of lists may be the correct structure for your program. You could
> define tablE as
>
> tablE= [[ 0, 2, 0, 0, 0, 0, 0],    # 1
>          [ 1, 3, 3, 0, 0, 0, 0],    # 2
>          [ 2, 0, 5, 2, 0, 0, 0],    # 3
> ...
>
>         [ 9, 0, 0,16, 0, 0, 0]]   # 19
>
> Kent
>

I'll give it a go, and see if I can make that work.
However, except for not getting 100% in my number
distribution routine, the dictionary seems to be working okay
(on my system).

Here's a simple example of the dictionary in action.
"for" loops have replaced all the "if" selections... this was the first example.
I've left the "if"'s in so the table.values() can be easily seen.

#!/usr/bin/python
# 2007-01-19
# 2007-01-20
"""
Map
+------+------+
|      |      |
|         5   |
|  1   |      |
|      +--  --+    N
|      |      |    |
+--  --+  4   | W--+--E
|      |      |    |
|      +--  --+    S
|  2   |      |
|         3   |
|      |      |
+------+------+
"""
#            rm#  N S E W
travelTable = {1:[0,2,5,0],
               2:[1,0,3,0],
               3:[4,0,0,2],
               4:[5,3,0,0],
               5:[0,4,0,1]}

def main():
    roomNum = 1
    print \
    """
    Instructions:
    Move through the rooms by pressing [N] for North, [S]
    for South, [E] for East, [W] for West, and [Q] to Quit.
    These movement keys are not case sensitive (ie. [n] is
    the same as [N]).
    """
    name = raw_input("What is your name? ")
    exploring=True
    while exploring:
        print name + ", your strength is",
        print str(strength) +"."
        #########################################################
        print name,"you are in room",roomNum
        #########################################################
        if travelTable.values()[roomNum-1][0] != 0:
            print "A door leads North."
        if travelTable.values()[roomNum-1][1] != 0:
            print "A door leads South."
        if travelTable.values()[roomNum-1][2] != 0:
            print "A door leads East."
        if travelTable.values()[roomNum-1][3] != 0:
            print "A door leads West."
        #########################################################
        move = raw_input("Which way do you want to go? ")
        if move.upper() == "Q":
            exploring=False
        #########################################################
        if move.upper()=="N" and travelTable.values()[roomNum-1][0] == 0:
            print
            print "You cannot move that way."
        if move.upper()=="S" and travelTable.values()[roomNum-1][1] == 0:
            print
            print "You can't walk through walls."
        if move.upper()=="E" and travelTable.values()[roomNum-1][2] == 0:
            print
            print "Try another direction."
        if move.upper()=="W" and travelTable.values()[roomNum-1][3] == 0:
            print
            print "There is no door to the West."
        #########################################################
        if move.upper() =="N" and travelTable.values()[roomNum-1][0] != 0:
            roomNum = travelTable.values()[roomNum-1][0]
        if move.upper() =="S" and travelTable.values()[roomNum-1][1] != 0:
            roomNum = travelTable.values()[roomNum-1][1]
        if move.upper() =="E" and travelTable.values()[roomNum-1][2] != 0:
            roomNum = travelTable.values()[roomNum-1][2]
        if move.upper() =="W" and travelTable.values()[roomNum-1][3] != 0:
            roomNum = travelTable.values()[roomNum-1][3]
        #########################################################
        print
main()

-- 
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