(a)

def main():
      maze= open('maze.txt','r+')
      for line in maze:
          line = line.strip()
          print line
       maze.close()
print
start(maze,x,y)
print maze[y][x]

(b)

def start(maze,x,y):
         for x in range(0,len(maze)):
                 if maze[y][x] == "s"
                 print maze[y][x]
                 return x, y

question (b): write a function that walks through the maze and attempts to find exit. As a minimum, the function should accept two argument that specify the starting row and column number.

Recommended Answers

All 7 Replies

def main():
      maze= open('maze.txt','r+')
      for line in maze:
          line = line.strip()
          print line
       maze.close()
print
start(maze,x,y)
print maze[y][x]

This code won't run. You don't call main() and neither maze, x, or y are defined before calling the start(maze, x, y) function. "How To Think Like A Computer Scientist" is a good learning tool; "Functions" Chapter (3.5) = http://www.greenteapress.com/thinkpython/html/book004.html

Maze = [ [ '#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#' ],\
[ '.','.','.','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#' ],\
[ '#','#','.','#','#','.','.','#','#','#','#','#','#','#','#','#','#','#','#','#' ],\
[ '#','#','.','#','#','.','.','#','#','#','#','#','#','#','#','#','#','.','.','.' ],\
[ '#','#','.','#','#','.','.','#','#','#','#','#','#','#','#','#','#','.','#','#' ],\
[ '#','#','.','.','.','.','#','#','#','#','#','#','#','#','#','#','#','.','#','#' ],\
[ '#','#','.','#','#','.','#','#','#','#','#','#','.','.','.','.','.','.','#','#' ],\
[ '#','#','.','#','#','.','#','#','#','#','#','#','.','#','#','#','#','.','#','#' ],\
[ '#','#','.','#','#','.','#','#','#','#','#','#','.','#','#','#','#','.','#','#' ],\
[ '#','#','#','#','#','.','.','.','.','.','.','.','.','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','.','#','#','#','#','#','#','#' ],\
[ '#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#' ],\
]

def PrintMaze():
global Maze
return Maze

print(PrintMaze())

I got dis, but I am not sure how to print Maze in rows shown above. it is coming as a single line.

def maze_to_string(maze):
    return "\n".join(''.join(row) for row in maze)

print maze_to_string(Maze)

"""my output --->
####################
####################
####################
...#################
##.##..#############
##.##..##########...
##.##..##########.##
##....###########.##
##.##.######......##
##.##.######.####.##
##.##.######.####.##
#####........#######
############.#######
############.#######
############.#######
############.#######
############.#######
############.#######
############.#######
####################

# if you replace '' with ' ', you get

# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
. . . # # # # # # # # # # # # # # # # #
# # . # # . . # # # # # # # # # # # # #
# # . # # . . # # # # # # # # # # . . .
# # . # # . . # # # # # # # # # # . # #
# # . . . . # # # # # # # # # # # . # #
# # . # # . # # # # # # . . . . . . # #
# # . # # . # # # # # # . # # # # . # #
# # . # # . # # # # # # . # # # # . # #
# # # # # . . . . . . . . # # # # # # #
# # # # # # # # # # # # . # # # # # # #
# # # # # # # # # # # # . # # # # # # #
# # # # # # # # # # # # . # # # # # # #
# # # # # # # # # # # # . # # # # # # #
# # # # # # # # # # # # . # # # # # # #
# # # # # # # # # # # # . # # # # # # #
# # # # # # # # # # # # . # # # # # # #
# # # # # # # # # # # # # # # # # # # #

"""

thanks

How can I move to row 4 and replace it with x, and move forward to col 2 of row 4.

x= "x"
Squares= 399
def start():
for i in range(squares):
Maze.append(x)
return Maze
print"\n\t", Maze[60], ",", Maze[61], ",", Maze[62]

print(start())

I am getting invalid sytamic for "\n\t"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.