python 方法破坏代码

问题描述 投票:0回答:1

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

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()

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

谢谢,洛根

python methods
1个回答
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 的合成器,例如:

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) 
© www.soinside.com 2019 - 2024. All rights reserved.