[Tutor] problem loading and array from an external file

Bill Allen wallenpb at gmail.com
Tue Aug 10 04:55:35 CEST 2010


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

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


More information about the Tutor mailing list