python方法破坏了代码

时间:2013-12-17 08:26:54

标签: python methods robot

我已经编写了一些代码,我希望将其转换为可导入的内容,而不是将其用作主要代码。

floorMap = [[000,000,000,000,000,999,999,999,999,999],
           [000,000,999,000,999,000,000,000,999,999],
           [000,000,999,000,999,000,000,000,999,999],
           [000,000,000,000,999,000,000,000,999,999],
           [999,000,000,000,999,000,999,999,999,999],
           [999,000,000,000,000,000,999,000,000,999],
           [999,000,000,000,999,999,999,000,000,999],
           [999,000,999,000,000,000,999,000,000,999],
           [999,000,999,999,999,000,000,000,000,999],
           [999,999,999,999,999,999,999,999,000,000]]


currentNum=0

wall=999
uncalculated=000

robotX=0
robotY=0
goalX=9
goalY=9

floorMap[goalY][goalX]=1

def changeSurroundings(X, Y):

        #left
        if(floorMap[X-1][Y]==uncalculated and X > 0):
            floorMap[X-1][Y]=currentNum   
        #right 
        if(X < len(floorMap[0])-1 and floorMap[X+1][Y]==uncalculated):
            floorMap[X+1][Y]=currentNum  
        #up
        if(floorMap[X][Y-1]==uncalculated and Y > 0):
            floorMap[X][Y-1]=currentNum  
        #down
        if(Y < len(floorMap)-1 and floorMap[X][Y+1]==uncalculated):
            floorMap[X][Y+1]=currentNum    


def printMap():
    i=0
    floorMap[goalY][goalX]='G'
    floorMap[robotY][robotX]='R'

    while(i<len(floorMap)):
        print floorMap[i]
        print ""
        i+=1
    print ""
    print ""

#------------------MOST IMPORTANT CHUNK OF CODE--------------

while(floorMap[robotY][robotX]==uncalculated):
    x=0
    while(x<len(floorMap[0])):
        y=0
        while(y<len(floorMap)):
            if(floorMap[x][y] > uncalculated and floorMap[x][y] < wall):
                currentNum=floorMap[x][y]+1
                changeSurroundings(x,y)                                
            y+=1
        x+=1

printMap()

我的问题是每当我尝试将最重要的代码块放在一个类似的方法中时;

def calcMap():  
while(floorMap[robotY][robotX]==uncalculated):
    x=0
    while(x<len(floorMap[0])):
        y=0
        while(y<len(floorMap)):
            if(floorMap[x][y] > uncalculated and floorMap[x][y] < wall):
                currentNum=floorMap[x][y]+1
                changeSurroundings(x,y)                                
            y+=1
        x+=1

printMap()

它破坏了我的代码。为什么?我似乎没有得到任何错误,它只是卡在其中一个嵌套循环上。我没有看到任何理由应该这样做,但你们可能会这样做;)

谢谢,洛根

1 个答案:

答案 0 :(得分:3)

您的问题来自您的全局变量,尤其是currentNum。这基本上就是你在做的事情:

current = 0

def f():
    current = 1
    g()

def g():
    print(current)

f()  # Output: 0

您需要做的是:

current = 0

def f():
    global current
    current = 1
    g()

def g():
    print(current)

f()  # Output: 1

或更好:

def f():
    current = 1
    g(current)

def g(current):
    print(current)

f()  # Output: 1

另外,您应该考虑为calcMap函数使用更加pythonic的synthax,例如:

def calc_map(): 
    while floor_map[robot_x][robot_y] == uncalculated :
        for x,array in enumerate(floor_map):
            for y,element in enumerate(array):
                if uncalculated < element < wall:
                    current_num = element + 1
                    change_surroundings(x, y, current_num)