Hello Devon, Here's a quick [untested] push in the direction of taking the code you gave below and modeling it as a class using an object-oriented design. With this code, you could then create an instance of a puzzle as:
toughOne = Sudoku() toughOne.play_game() Within the class definition, variables that you need to have shared between the differnt functions are qualified as instance variables using the syntax self.blah (as in self.puzzle and self.new_puzzle). Unqualified variables are local to an individual function and cannot be shared. With regard, Michael class Sudoku: def __init__(self): self.puzzle = [[1,2,3,4,5,6,7,8,9], [4,5,6,7,8,9,1,2,3], [7,8,9,1,2,3,4,5,6], [2,3,4,5,6,7,8,9,1], [5,6,7,8,9,1,2,3,4], [8,9,1,2,3,4,5,6,7], [3,4,5,6,7,8,9,1,2], [6,7,8,9,1,2,3,4,5], [9,1,2,3,4,5,6,7,8]] num_of_swap = random.randint(10,20) for i in range(num_of_swap): row1 = random.randint(0,8) row2 = random.randint(0,8) if row1/3 == row2/3: self.swap_row(row1,row2) self.new_puzzle = copy.deepcopy(puzzle) sparseness = 0.85 for i in range(9): for j in range(9): if random.uniform(0,1) < sparseness: self.new_puzzle[i][j] = '' def swap_row(self,row1,row2): temp = self.puzzle[row1] self.puzzle[row1] = self.puzzle[row2] self.puzzle[row2] = temp def play_game(self): '''Here is where I need the variables 'puzzle' and 'new_puzzle' to be brought.''' pass +----------------------------------------------- | Michael Goldwasser | Associate Professor | Dept. Mathematics and Computer Science | Saint Louis University | 220 North Grand Blvd. | St. Louis, MO 63103-2007 | | Office: Ritter Hall 6 | Email: goldwamh at slu dot edu | URL: euler.slu.edu/~goldwasser On Wednesday November 28, 2007, Devon MacIntyre wrote: > Hi, > > I have two functions, 'new_sudoku' and 'play_game'. In new_sudoku, I have a > pre-determined puzzle (I wasn't able to get a randomly generated puzzle > working), as a matrix in the variable 'puzzle'. I imported the 'copy' > module > and made a deep-copy of 'puzzle' to make 'new_puzzle', which randomly has > 85% of the digits replaced by an empty string. Now that 'new_puzzle' is > only > 15% filled with numbers, I use turtle to place the numbers on a grid that I > made (also with turtle). After the grid and 'new_puzzle' are generated, I > ask the player if he/she wants to begin playing. If yes, then the function > 'play_game' is started. Here, I'm going to let the player choose spots to > input their own numbers to fill in the board. My problem is that I can't > get > the variables 'puzzle' and 'new_puzzle' into that function (to be compared) > because they are not globally defined; only in 'new_sudoku' function. > Here's > some selected code from my program: > > def swap_row(puzzle,row1,row2): > temp = puzzle[row1] > puzzle[row1] = puzzle[row2] > puzzle[row2] = temp > > def new_sudoku(): > puzzle = [[1,2,3,4,5,6,7,8,9], \ > [4,5,6,7,8,9,1,2,3], \ > [7,8,9,1,2,3,4,5,6], \ > [2,3,4,5,6,7,8,9,1], \ > [5,6,7,8,9,1,2,3,4], \ > [8,9,1,2,3,4,5,6,7], \ > [3,4,5,6,7,8,9,1,2], \ > [6,7,8,9,1,2,3,4,5], \ > [9,1,2,3,4,5,6,7,8]] > num_of_swap = random.randint(10,20) > for i in range(num_of_swap): > row1 = random.randint(0,8) > row2 = random.randint(0,8) > if row1/3 == row2/3: > swap_row(puzzle,row1,row2) > new_puzzle = copy.deepcopy(puzzle) > sparseness = 0.85 > for i in range(9): > for j in range(9): > if random.uniform(0,1) < sparseness: > new_puzzle[i][j] = '' > > def play_game(): > ''' > Here is where I need the variables 'puzzle' and 'new_puzzle' to be > brought. > ''' _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor