0

I've started learning python and currently working on making a simple Sudoku game.

I have 2 modules sudoku.py and sudoku_GUI.py

sudoku.py contains one class - Board and it's purpose is to solve the game based on an input.

solve method, in order to generate solution, needs to call other methods:

def solve(self):
 empty_fields = self.find_empty()
 if empty_fields == []:
 return True
 self.init_rows_columns_squares()
 row, column = empty_fields[0]
 square = 3 * (row//3) + column//3
 valid_numbers = list(set(Board.number_arr) - (set(self.rows[row] + self.columns[column] + self.squares[square])))
 valid_numbers.sort()
 for i in valid_numbers:
 if not self.is_valid(empty_fields[0], i):
 return False
 self.insert_number(empty_fields[0], i)
 if self.solve(): 
 return True
 self.insert_number(empty_fields[0], 0)
 return False

And this works just fine when it is called from its own module. However, when I try to call it from Sudoku_GUI:

import sudoku
def solve_board(self, board):
 board.solve()
board_1 = sudoku.Board(sudoku.board)
solve_board(self, board_1)

It doesn't work, solve method gets called and it won't call other methods as it does in its own module. Obviously, I could solve it in first module and just import solved board, but since I'm learning I want to know why this happens.

If someone could explain this to me, I would be very greatful. Thank you!

Barmar
789k57 gold badges555 silver badges669 bronze badges
asked Jan 30, 2020 at 0:05
11
  • 2
    what is the error you see? Commented Jan 30, 2020 at 0:09
  • There is no error, it runs, just won't call self.init_rows_columns_squares() and other I tested it by putting print("method_name") in every method and it just prints "solve" Commented Jan 30, 2020 at 0:11
  • 1
    paste your whole code to paste.ubuntu.com and send short link here Commented Jan 30, 2020 at 0:12
  • Ok, will do, just a sec Commented Jan 30, 2020 at 0:12
  • Does it work correctly if everything is in a single file? Commented Jan 30, 2020 at 0:13

1 Answer 1

1

From the code you wrote, i understand you are calling your functions in a wrong way! you only use self keyword when you are writing code in a class. outside the class you don't need to write self when you call the class methods.

i will rewrite your code in order to work :

>>> import sudoku
>>> from pprint import pprint
>>>
>>> class test_gui:
... def solve_board(self, board):
... board.solve()
...
>>> board_1 = sudoku.Board(sudoku.board)
>>> gui = test_gui()
>>> gui.solve_board(board_1)
>>> pprint(board_1.board)
[[1, 4, 6, 7, 9, 2, 3, 8, 5],
 [2, 5, 8, 3, 4, 6, 7, 9, 1],
 [3, 7, 9, 5, 8, 1, 4, 6, 2],
 [4, 3, 7, 9, 1, 5, 8, 2, 6],
 [5, 8, 1, 6, 2, 7, 9, 3, 4],
 [6, 9, 2, 4, 3, 8, 1, 5, 7],
 [7, 1, 3, 2, 6, 9, 5, 4, 8],
 [8, 2, 4, 1, 5, 3, 6, 7, 9],
 [9, 6, 5, 8, 7, 4, 2, 1, 3]]
answered Jan 30, 2020 at 0:34
Sign up to request clarification or add additional context in comments.

3 Comments

@ Mohsen_Fatemi Nicely spotted, but I do not think that is the issue, because that call would raise exception of some kind, if I am right? I have scanned my code now, it is a typo. I will correct that now. Sorry about that and thank you for time you are commiting to my issue.
yw, it does not raise exception btw. if you see this as a true answer mark it as answer. thanks.
Marking now, you helped me find the solution. Just found that insted for "" I was looking for 0s in IF which makes sense in console version but in GUI I want blanks. Thanks once again!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.