Python - exact same loop works inconsistently 'int' object is not iterable - not iterating over int -
i need run loop twice in function 2 different things. works fine in 1 place fails run in other, though there no discernible difference between two.
this loop
for each in problem.getsuccessors(coord): #each in [((x,y), 'dir', num)...]
it runs fine second time, giving me correct solution,
def recursivedfs(coord, stck, actions, visited, problem): repush = false print "iterate", problem.getsuccessors(coord) each in problem.getsuccessors(coord): #each in [((x,y), 'dir', num)...] if not each[0] in visited: repush = true if repush: stck.push(coord) if problem.isgoalstate(coord): print coord, " goal state" return stck else: if coord in visited: print "revisited ", coord else: visited.append(coord) print "visited ", coord print "iterate", problem.getsuccessors(coord) each in problem.getsuccessors(coord): #each in [((x,y), 'dir', num)...] if not each[0] in visited: print "pushed ", each stck.push(each) return recursivedfs(each[0], stck, actions, visited, problem) new = stck.pop() print "popped ", new return recursivedfs(new[0], stck, actions, visited, problem)
here screenshots detailing error. can't imagine what's different between first loop , second.
when uncomment first, seems think iterating on int.
what's difference?
some additional info error on line 197 of searchagents: addressing function getsuccessors, should return attempting iterate over. should (and in case of second loop is) tuple containing: [(x, y), "direction", int] x , y ints.
def getsuccessors(self, state): successors = [] action in [directions.north, directions.south, directions.east, directions.west]: x,y = state dx, dy = actions.directiontovector(action) nextx, nexty = int(x + dx), int(y + dy) if not self.walls[nextx][nexty]: nextstate = (nextx, nexty) cost = self.costfn(nextstate) successors.append( ( nextstate, action, cost) ) # bookkeeping display purposes self._expanded += 1 # not change if state not in self._visited: self._visited[state] = true self._visitedlist.append(state) return successors
edited: looked on issue not in loop, part right below first loop pushes coord
stck
.
if repush: stck.push(coord)
this coordinate instead of each
intended push, means @ end of function when pop out calling new
fails since coord. can't suggest full solution since i'm not sure majority of code doing why output has "popped (3,5)" right before error raised , why first element of integer instead of coordinate.
Comments
Post a Comment