0

I'm writing a program for creating a puzzle solver. I have created a class that defines a board and has the init and get function. I am then defining a function that initializes a board based on two inputs. The board is a list of lists and each list contains numbers 1 - n depending on the input except the bottom right is 0. For some reason when I attempt to return an object of the class in my function, it tells me the class is not defined. (global name 'TilePuzzle' is not defined)

def create_tile_puzzle(rows, cols):
    count = 1
    b = [[] for x in range(rows)]
    for i in range(rows):
         for j in range(cols):
            b[i].append(count)
            count += 1
    b[rows-1][cols-1] = 0
    print b
    return TilePuzzle(b)

print create_tile_puzzle(4, 4)


class TilePuzzle(object):
    def __init__(self, board):
        self.board = board

    def get_board(self):
        return self.board
greenteam
  • 305
  • 5
  • 16
  • Briefly: At the point where you try to execute that code, that name is not yet defined. Python is interpreted rather than compiled, and doesn't hoist function definitions. Simply put your class definition before where you plan to invoke it. – TigerhawkT3 Oct 03 '16 at 00:54
  • @TigerhawkT3 Thanks, that actually fixed the problem. However I have used similar format where I used a class before the definition and it has worked fine. Is there a reason for that? I've never had this problem before – greenteam Oct 03 '16 at 01:00

0 Answers0