Reputation: 4460
I am getting a strange incorrect indentation error that I cannot track down. As far as I know, I did not modify this file since last time it was correctly working.
Edit: Ok it turns out I did modify it unknowingly (obviously something had to have changed since last time). I hit a hotkey in my editor which had turned some tabs into spaces. Thanks!
The error is on line 100: ctrl f for "###"
#!/usr/bin/env python
import sys
from collections import deque #high performance queue "deck"
class Node(object):
def __init__(self,x,y,history):
self.locationx = x
self.locationy = y
self.data = None
self.history = history #previous node to go backwards
def printNodePathTrace(inNode,width,height,mapTerrain,frontier):
#travel backward through node history until history == None is reached
#print off map of path
mapPath = mapTerrain
for i in range(width): #fill map with blanks
for j in range(height):
mapPath[i][j] = '-'
#print frontier list
print "length of frontier"
print len(frontier)
for item in frontier:
#print item.locationy
#print item.locationx
mapPath[item.locationy][item.locationx] = '*'
#print path found
done = 0
count = 0
currentNode = inNode
while(done == 0 and count < 50):
mapPath[currentNode.locationy][currentNode.locationx] = '#'
if currentNode.history == None:
done = 1
currentNode = currentNode.history
count += 1
printMap(mapPath)
def printMap(mapTerrain): #horizontal right positive x, verticle down positive y
for i in mapTerrain:
for j in i:
sys.stdout.write(j)
sys.stdout.write('\n')
def printMapStartAndGoal(mapTerrain,startX,startY,goalX,goalY):
#Y is row, X is column. Y is vertical, X is horizontal
temp1 = mapTerrain[startY][startX]
temp2 = mapTerrain[goalY][goalX]
mapTerrain[startY][startX] = 'S'
mapTerrain[goalY][goalX] = 'G'
printMap(mapTerrain)
mapTerrain[startY][startX] = temp1
mapTerrain[goalY][goalX] = temp2
def main():
#Input map
#Your program should be able to read a map file in the following format.
#Width Height
#StartX StartY
#GoalX GoalY
#map
searchMode = "BFS" #options are BFS, LC, ID, A*1, A*2
logfile = open("smallmap2.txt", "r")
[width,height] = map(int,logfile.readline().split())
[startX,startY] = map(int,logfile.readline().split())
[goalX,goalY] = map(int,logfile.readline().split())
mapTerrainInput = logfile.read()
mapTerrain = map(list,mapTerrainInput.splitlines())
#map the list function to mapTerrainInput split into lines without '\n'
printMapStartAndGoal(mapTerrain,startX,startY,goalX,goalY)
print mapTerrain
printMap(mapTerrain)
closedList = [] #contains list of nodes visited already
frontier = deque([])
startNode = Node(startX,startY,None)
#check if node is a goal node
#add node to closed list
#add expansions to frontier list (not ones on closed list)
#Repeat with next node in Frontier
goalFound = 0 ### there's an error with this line's indentation???
iterationCount = 0
currentNode = startNode
while goalFound == 0 and iterationCount < 500: #stop when goal is found
if (currentNode.locationx == goalX and currentNode.locationy == goalY):
goalFound = 1
break
closedList.append(currentNode)
#expand node - currently not checking the closed list
if (currentNode.locationy > 0): #can expand up
frontier.append(Node(currentNode.locationx,currentNode.locationy - 1,currentNode))
if (currentNode.locationy < height - 1): #can expand down
frontier.append(Node(currentNode.locationx,currentNode.locationy + 1,currentNode))
if (currentNode.locationx > 0): #can expand left
frontier.append(Node(currentNode.locationx - 1,currentNode.locationy,currentNode))
if (currentNode.locationx < width -1): #can expand right
frontier.append(Node(currentNode.locationx + 1,currentNode.locationy,currentNode))
#done expanding
currentNode = frontier.popleft()
iterationCount += 1
print currentNode.history
print currentNode.locationx
print currentNode.locationy
printNodePathTrace(currentNode,width,height,mapTerrain,closedList)
if __name__ == '__main__':
main()
Upvotes: 0
Views: 2579
Reputation: 837966
I copied and pasted your code from StackOverflow and it does not have the error. ideone
Usually this error is caused by mixing of tabs and spaces. Looking at the source for your code I see the problem. Here's how your code looks in Visual Studio with whitespace visible:
Solution
Convert the tabs to spaces.
Upvotes: 6
Reputation: 208405
As other answers have mentioned, your issue is that you are mixing tabs and spaces, here is some proof:
I took this screenshot while "editing" your answer, and then searched for four spaces. All occurrences of four spaces are highlighted in yellow.
Note that the spacing immediately before goalFound = 0
is highlighted, but previous lines have non-highlighted spacing (indicating that tabs were used).
You should never mix tabs and spaces in your indentation because it can be hard to catch errors like this. Python treats tabs as eight spaces, but depending on your editor tabs may look equivalent to 4 spaces (or some other number). So even though your code looks like it is correctly indented, what Python actually sees looks like this:
def main():
#... all previous lines used tabs
#Repeat with next node in Frontier
goalFound = 0
Upvotes: 5
Reputation: 11915
4 is the number of spaces we will use to indent. 3 is too few, 5 is too many. 8 is right out! Although, really old code that uses 8 is OK.
http://www.python.org/dev/peps/pep-0008/
Upvotes: 1
Reputation: 6441
Do you have:
Upvotes: 2
Reputation: 35907
You are most likely mixing tabulations and spaces. Choose one type and stick with it.
Upvotes: 3