[Tutor] problem loading and array from an external file

Bill Allen wallenpb at gmail.com
Tue Aug 10 16:42:24 CEST 2010


On Tue, Aug 10, 2010 at 8:21 AM, bob gailer <bgailer at gmail.com> wrote:
> On 8/9/2010 11:13 PM, Bill Allen wrote:
>>
>> Bob,
>>
>> Thanks for the feedback.  I have been stepping through it in the IDLE
>> debugger.  However, it may be that I am letting the debugger get in my
>> way as I am new to it also.  You are right.   I'll get out the old
>> yellow legal pad and step through it by hand.  In fact, I think I will
>> process the loop entirely by hand, just the logic, and see how the
>> variables look that way.
>>
>>
>
> Glad I could help.
>

Bob,

I was really off on that algorithm and had way over complicated it.
I have it working correctly now, but for the sake of those who saw my
earlier really botched code, here is the resultant code that works.
The entire inner loop and intermediate variables are removed, but
shown commented out.  I had somehow thought I needed to read each
member of each subarray in individually.  That was not the case and
that inner loop was overwriting the array.

# reads one line at a time from file and puts data into array
    for line in textf:
        #tempwords = line.split(None)
        #for n in range(0, len(room)-1):
        #    roomx[n] = tempwords[n]
        #room[m] = roomx
        room[m] = line.split(None)
        m += 1




>
>> Thanks,
>> Bill
>>
>>
>> On Mon, Aug 9, 2010 at 10:08 PM, bob gailer<bgailer at gmail.com>  wrote:
>>
>>>
>>> On 8/9/2010 10:55 PM, Bill Allen wrote:
>>>
>>>>
>>>> Hi, my name is Bill and I am completely stuck.  I have a little bit of
>>>> code I am working with for the sole purpose of coding in order to
>>>> learn Python.  In order to learn a programming language, I very often
>>>> use a little text based map of rooms traversal game.  So far, so good.
>>>>  However, this time I decided to learn some file operations so I am
>>>> trying to read the data in from an external file rather than
>>>> preloading an array inside the code itself.  I am able to open the
>>>> file and read from it.  However, I am getting unexpected results when
>>>> filling the array.  Instead of each row in the array being loaded with
>>>> the matching line in the data file, by the end of the loops all the
>>>> rows in the array end up being filled with the last line of the data
>>>> file.   I have stepped and traced through it and cannot figure out
>>>> where I am going wrong.
>>>>
>>>
>>> How did you step thru it? If you do it very precisely the problem will be
>>> evident.
>>> I'd prefer you discover it by careful step-thru rather than feeding you
>>> the
>>> answer.
>>>
>>>
>>>>
>>>> This may not be a Python issue at all, but is
>>>> likely me being way off in the weeds on my algorithm.  This is only
>>>> the portion of the code for loading the array from the file.  I am
>>>> writing this in Python 3.  I believe my problem is somewhere in the
>>>> read_maze_data_file() function.
>>>>
>>>
>>> That is accurate.
>>>
>>>
>>>>
>>>> There are lots of unnecessary extra
>>>> print statements in this code because I was trying to carefully follow
>>>> the state of the variables as the code ran.  If anyone can point out
>>>> where I am going wrong, I would really appreciate it.
>>>>
>>>>
>>>
>>> As mentioned above you have a bug in your step-throuogh process. Write
>>> down
>>> the variables that are affected by each statement and note their new
>>> values.
>>>
>>>>
>>>> The code:
>>>> #          N S E W U D
>>>> room0 = [0,0,0,0,0,0,0]
>>>> room1 = [0,0,0,0,0,0,0]     #Pad the first place so that rooms may
>>>> room2 = [0,0,0,0,0,0,0]     #be referenced naturally
>>>> room3 = [0,0,0,0,0,0,0]
>>>> room4 = [0,0,0,0,0,0,0]          #First places will have special
>>>> purposes
>>>> room5 = [0,0,0,0,0,0,0]
>>>> room6 = [0,0,0,0,0,0,0]          #initialize the array with zeros
>>>> room7 = [0,0,0,0,0,0,0]
>>>>
>>>> room = [room0,room1,room2,room3,room4,room5,room6,room7]
>>>>
>>>>
>>>> def clearscreen(numlines=100):
>>>>    """Clear the console.
>>>>    numlines is an optional argument used only as a fall-back.
>>>>    """
>>>>    import os
>>>>    if os.name == "posix":
>>>>        # Unix/Linux/MacOS/BSD/etc
>>>>        os.system('clear')
>>>>    elif os.name in ("nt", "dos", "ce"):
>>>>        # DOS/Windows
>>>>        os.system('CLS')
>>>>    else:
>>>>        # Fallback for other operating systems.
>>>>        print('\n' * numlines)
>>>>    print(os.name)
>>>>
>>>> def print_map():
>>>>    print("+-------------+")
>>>>    print('|','MAP:   ','N','S','E','W','U','D','|', sep="", end="")
>>>>    for x in range(1,8):
>>>>        print()
>>>>        print("|","room",x,">    ", sep="", end="")
>>>>        for y in range(1,7):
>>>>            print(room[x][y], end="")
>>>>        print("|", end="")
>>>>    print()
>>>>    print("+-------------+", end="")
>>>>
>>>>
>>>> def read_maze_data_file():
>>>>
>>>>    roomx = [0,0,0,0,0,0,0]
>>>>    n, m = 0, 0
>>>>
>>>>    try:
>>>>      filename = 'mazegame.dat'
>>>>      textf = open(filename, 'r')
>>>>    except IOError:
>>>>      print ('Cannot open file %s for reading' % filename)
>>>>      import sys
>>>>      sys.exit(0)
>>>>
>>>> # reads one line at a time
>>>>
>>>>    for line in textf:
>>>>        print("raw line in file: ",line, end="")
>>>>        tempwords = line.split(None)
>>>>        print ("line as read from file: ",tempwords)
>>>>
>>>>        for n in range(0, len(room)-1):
>>>>            roomx[n] = tempwords[n]
>>>>            #print(roomx[n])
>>>>
>>>>        print("roomx",roomx)
>>>>        room[m] = roomx
>>>>        print("room ",m,room[m])
>>>>        print("current state of room array")
>>>>        print("room 0",room[0])
>>>>        print("room 1",room[1])
>>>>        print("room 2",room[2])
>>>>        print("room 3",room[3])
>>>>        print("room 4",room[4])
>>>>        print("room 5",room[5])
>>>>        print("room 6",room[6])
>>>>        print("room 7",room[7])
>>>>
>>>>        m += 1
>>>>
>>>>    textf.close()
>>>>    return(room)
>>>>
>>>> #----END read_maze_data_file()
>>>>
>>>>
>>>> #---------MAIN SECTION---------
>>>>
>>>> clearscreen()
>>>>
>>>>
>>>> print("LOAD AN ARRARY FROM A FILE")
>>>> print("              by Bill Allen")
>>>> print()
>>>> print("initial state of room array")
>>>> print(room)
>>>> print()
>>>>
>>>> print("data from file")
>>>> room_final = read_maze_data_file()
>>>> print("from MAIN")
>>>> print(room_final)
>>>> print()
>>>>
>>>> print("a PRINT_MAP call from MAIN")
>>>> print_map()
>>>> print()
>>>> print()
>>>>
>>>>
>>>> =======================
>>>> partial sample output showing the current incorrect results:
>>>> a PRINT_MAP call from MAIN
>>>> +-------------+
>>>> |MAP:   NSEWUD|
>>>> |room1>    000050|
>>>> |room2>    000050|
>>>> |room3>    000050|
>>>> |room4>    000050|
>>>> |room5>    000050|
>>>> |room6>    000050|
>>>> |room7>    000050|
>>>> +-------------+
>>>> =========================
>>>> the contents of the data file, mazegame.dat
>>>> 0 0 0 0 0 0 0
>>>> 0 0 4 2 0 0 0
>>>> 0 0 0 1 3 6 0
>>>> 0 0 5 0 2 0 0
>>>> 0 1 0 5 0 0 0
>>>> 0 3 0 0 4 0 7
>>>> 0 0 0 0 0 0 2
>>>> 0 0 0 0 0 5 0
>>>> _______________________________________________
>>>> Tutor maillist  -  Tutor at python.org
>>>> To unsubscribe or change subscription options:
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>>>
>>>>
>>>
>>> --
>>> Bob Gailer
>>> 919-636-4239
>>> Chapel Hill NC
>>>
>>>
>>>
>>
>>
>
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
>


More information about the Tutor mailing list