python中的未知错误

时间:2012-02-10 21:02:44

标签: python

我收到一个奇怪的错误缩进错误,我无法追查。据我所知,自上次正确使用以来,我没有修改过这个文件。

编辑:好的,事实证明我确实在不知情的情况下对其进行了修改(显然自上次以来必须改变一些事情)。我在编辑器中点了一个热键,它将一些标签变成空格。谢谢!

错误在第100行:ctrl f代表“###”

#!/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()

5 个答案:

答案 0 :(得分:6)

我从StackOverflow复制并粘贴了您的代码,但没有错误。 ideone

通常此错误是由标签和空格的混合引起的。查看代码的来源,我看到了问题所在。以下是您的代码在Visual Studio中的外观可见空白:

enter image description here

<强>解决方案

将标签转换为空格。

  • 如果您的编辑器可以选择将标签转换为空格,请使用它,您将看到问题。
  • 如果您的编辑器允许您将标签宽度更改为8,请执行此操作,您将看到问题。
  • 如果您的编辑器可以选择显示空白字符,请尝试将其打开。
  • 否则尝试删除错误附近所有行中的所有前导空格并再次添加空格(使用空格键,只是为了确定)。
  • 或者只是将您的代码从StackOverflow复制回文件并保存,因为StackOverflow似乎已经为您修复了它。

答案 1 :(得分:5)

正如其他答案所提到的,你的问题是你正在混合标签和空格,这里有一些证据:

enter image description here

我在“编辑”你的答案时拍了这个截图,然后搜索了四个空格。所有出现的四个空格都以黄色突出显示。

请注意,紧接goalFound = 0之前的间距会突出显示,但前一行的间距不会突出显示(表示已使用制表符)。

你不应该在缩进中混合制表符和空格,因为很难捕捉到这样的错误。 Python将制表符视为八个空格,但根据您的编辑器标签,可能看起来相当于4个空格(或其他一些数字)。因此,即使您的代码看起来正确缩进,Python实际看到的内容如下所示:

def main():
        #... all previous lines used tabs
        #Repeat with next node in Frontier

    goalFound = 0

答案 2 :(得分:3)

您最有可能混合制表和空格。选择一种类型并坚持下去。

答案 3 :(得分:2)

你有:

  1. 在标记行
  2. 之前的空白行上的无关空格
  3. “startNode = Node(...”
  4. 之后的空白行上的无关空格
  5. “goalFound = 0”之前的标签和空格的混合?

答案 4 :(得分:1)

4是我们将用于缩进的空格数。 3太少,5太多。 8出来了!虽然,使用8的真正旧代码是可以的。

http://www.python.org/dev/peps/pep-0008/