Python 程序在 return 语句之前的列表中有值用打印检查,但原始调用方法在 return

问题描述

我正在为学校编写一个简单的迷宫解决程序。该方法应该从镜子反射一束光线(-1 是 -45 度,1 是 45 度,0 是空白空间)并且由于某种原因我无法解决最后一个小问题;退出案例。

删除了移动方向方法,因为 SO 说我有太多代码。如果需要,我可以包含它。

当最后一步从 moveRight 方法进入 isAngled 时,列表具有完整路径。我可以在那里打印它或使用最后两种方法中的任何一种访问最终列表的索引。然而,当我从 isAngled 返回时,我在 maze_solver 中的列表是空的,我很难明白为什么。

我尝试在线阅读并进行了大量更改,我使用的是 jupyterlab notebook、windows 10、python 3.8.3

这是代码,感谢您的帮助!

#oreintation denotes direction light is headed 1: right,2: left,3:down,4:up 5:end of maze
#loc[0] is x
#loc[1] is y

#helper functions for moving in a given direction
def moveRight(maze,loc,path):
    if loc[1]+1 < len(maze[0]):
        isAngled(path,maze,(loc[0],loc[1]+1),maze[loc[0]][loc[1]+1],1)
    else:
        return isAngled(path,loc[1]),maze[loc[0]][loc[1]],5)
        #print(path)

def moveLeft(maze,path):
    if loc[1]-1 >= 0:
        isAngled(path,loc[1]-1),maze[loc[0]][loc[1]-1],2)
    else:
        return isAngled(path,5)

def moveDown(maze,path):
    if loc[0]+1 < len(maze):
        isAngled(path,(loc[0]+1,maze[loc[0]+1][loc[1]],3)
    else:
        return isAngled(path,5)
        print(path)
def moveUp(maze,path):
    if loc[0]-1 >= 0:
        isAngled(path,(loc[0]-1,maze[loc[0]-1][loc[1]],4)
    else:
        return isAngled(path,5)
        
#add location to the route,then check if it contains a mirror and turn accordingly
def isAngled(path,nextNum,orientation):
    #path.append(loc)
    
    if orientation ==5: #this is my exit case
        print(path[3]) #My full list is still available here
        return path
    else:
        path.append(loc)
    
    #moving to the right
    if  (nextNum) < 0 and orientation == 1:
        moveDown(maze,(loc),path)
    elif (nextNum) > 0 and orientation ==1:
        moveUp(maze,path)
    elif orientation == 1:
        moveRight(maze,path)
    #moving to the left  
    if (nextNum) < 0 and orientation == 2:
        moveUp(maze,path)
    elif (nextNum) > 0 and orientation ==2:
        moveDown(maze,path)
    elif orientation ==2:
        moveLeft(maze,path)
    #moving downawrds
    if (nextNum) > 0 and orientation == 3:
        moveLeft(maze,path)
    elif (nextNum) < 0 and orientation ==3:
        moveRight(maze,path)   
    elif orientation ==3:
        moveDown(maze,path)
    #moving upwards
    if nextNum > 0 and orientation ==4:
        moveRight(maze,path)
    elif nextNum < 0 and orientation ==4:
        moveLeft(maze,path)
    elif orientation == 4:
        moveUp(maze,path)
        
#execute the program. maze always starts at (0,0) and heading to the right.
def maze_solver(maze):
    path = []
    newpath = isAngled(path,(0,0),maze[0][0],1) #enter maze headed right,check if first square is a mirror
    #newpath,or the value of this isangled method call are both none here
    return newpath
    
    
maze = [
[ 0,-1],[ 1,0],[ 0,1,[-1,1]]
done = maze_solver(maze)
print(done)

输出: (0,3)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)