Any help would greatly appreciated. Question: I am getting the following error from the validMove function: ValueError: too many values to unpack
This program (currently) consists of a 2dArray and objects (represented by integers) in that array that can be moved. I am guessing that it has to do with copying a 2dArray to a local member value. I thought that it was just copying a pointer to the array and not the entire array, but I am not sure anymore. To tell the truth I have decided to change how this whole thing is done, but I really want to learn why this didn't work before I do. There are three sections - code - command line example of error - conceptual example if code doesn't make sense ======================================================================== ======= class WorldObject: def __init__(self): s = self s.curPos = [0,0] def createRandomPosition(s,arrayEnv): """takes a 2d array and returns a random position for placement of object. currentPosition contains 2 integers for column and row.""" s.curPos[0] = random.randint(0,len(arrayEnv)-1) s.curPos[1] = random.randint(0,len(arrayEnv[0])-1) return s.curPos def validMove(s,arrayEnv,newPosition): """calls to see if newPosition is inside the bounds of the 2d environment array and then if the newPosition is not a wall or something that the object can not go through (unblocked)""" s,arrayEnv = arrayEnv####### THIS MIGHT BE MY PROBLEM Is it just copying a pointer?####### s.newPos = newPosition #newPosition for example is [0,1] to go East or [-1,-1] to go NorthWest if s.insideArray(s.arrayEnv,s.newPos) and s.unblocked(s.arrayEnv,s.newPos): s.curPos[0] += s.newPos[0] s.curPos[1] += s.newPos[1] return s.curPos else: return curPos #returns the same position that was evaluated w/o changes def insideArray(s, arrayEnv,newPosition): """called by validMove to make sure I don't get an out bounds error for newPosition""" s.newPos = newPosition#so I can fit everything on one line if s.curPos[0] + s.newPos[0] < 0 or s.curPos[0] + s.newPos[0] >= len(arrayEnv[0]): return False elif s.curPos[1] + s.newPos[1] < 0 or s.curPos[1] + s.newPos[1] >= len(arrayEnv): return False else: return True def unblocked(s,arrayEnv,newPosition): """called by validMove to make sure that I am not walking through walls. For ease of explanation arrayEnv just holds integers. If the integer is >= 5 then I can't walk through it""" s.newPos = newPosition if arrayEnv[s.curPos[0] + s.newPos[0]][s.curPos[1] + s.newPos[1]] < 5: return True elif arrayEnv[s.curPos[0] + s.newPos[0]][s.curPos[1] + s.newPos[1]] >= 5: return False else: print "error in World Object unblocked function" return False ==================================COMMAND LINE ERROR=================================== >>> env = [[0,0,0],[0,0,0],[0,0,0]] >>> apple = WorldObject() >>> apple.createRandomPosition(env) [1, 2] >>> newPosition = [0,0] #not moving anywhere at all >>> apple.insideArray(env,newPosition) True >>> apple.unblocked(env,newPosition) True >>> apple.curPos [1, 2] >>> apple.validMove(env,newPosition) Traceback (most recent call last): File "<input>", line 1, in ? File "<input>", line 27, in validMove ValueError: too many values to unpack ======================CONCEPTUAL EXAMPLE IF CODE DOESN'T MAKE SENSE======================= env = [[0,0,0],[0,0,0],[0,0,0]] currentPosition = [1,1] WorldObject is represented by the number 7 env[ currentPosition[0] ][ currentPosition[1] ] = 7 env = [[0,0,0],[0,7,0],[0,0,0]] newPosition = [0,0] by adding currentPosition[0] to newPosition[0] I get the new coordinates in the env array. same with currentPosition[1] to newPosition[1] I just have to check to see if I am within the array so I don't go out of bounds, and to make sure I am not running into a wall. For example if env[0][0] is 5 (integer represents wall) then don't move James Carnell
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor