-3
grid = [[0, 4, 8, 2, 0, 0 ,0, 0, 1],
        [1, 0, 0, 3, 8, 4, 7, 2, 6],
        [3, 0, 0, 7, 0, 1, 9, 4, 8],
        [0, 7, 2, 6, 4, 5, 1, 8, 0],
        [8, 0, 0, 0, 0, 2, 4, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 7],
        [0, 8, 4, 0, 0, 0, 3, 0, 0],
        [0, 8, 4, 0, 0, 0, 3, 0, 0],
        [6, 0, 0, 4, 1, 0, 0, 0, 2],
        [0, 0, 3, 0, 0, 0, 0, 7, 4]]
import numpy as np

print(np.matrix(grid))

def possible(y, x, n) :
    global grid
    for i in range(0, 9):
        if grid[y][i] == n:
            return False
    for i in range(0, 9):
        if grid[i][x] == n:
            return False
    x0 = (x//3)*3
    y0 = (y//3)*3
    for i in range(0,3) :
        for j in range(0,3):
            if grid[y0+i][x0+j] == n:
                return False
    return True

def solve():
    global grid
    for y in range(9):
        for x in range(9):
            if grid[y][x] == 0:
                for n in range(1, 10):
                    if possible(y, x, n):
                        grid[y][x] = n
                        solve()
                        grid[y][x] = 0
                return
    print(np.matrix(grid))
    input('more?')

hello. I am a beginner. This is just my third day of using python. I am also new to this forum. I decided to do some sudoku solver and followed someone on youtube. And learn the thinking process behind it. I followed it through and through. But got different results. the output is printing the grid. by it seem the function solve() is not.

I hope I am not wasting your time. I am just a beginner that wants to learn.( I am using psycharm, if that counts)

Thank you.

enzo
  • 9,861
  • 3
  • 15
  • 38
  • 1
    One reason it doesn't work is you don't call `solve`. If you want to learn, learn to use a debugger. – TomServo Jun 16 '21 at 23:42
  • @TomServo how do i call to solve. Sorry if it is a stupid question. I tried print(solve()) it showed none. – Asher Paul Tan Jun 16 '21 at 23:46
  • 4
    If wherever you're learning python recommends using the `global` keyword then may I suggest you look for a different python learning resource. –  Jun 16 '21 at 23:46
  • 2
    It is not a stupid question, but this isn't a tutorial site, and we're not tutors. Complete practically any python tutorial online or (!) from a book and you'll see. Also, again, learn to use a debugger. – TomServo Jun 16 '21 at 23:48
  • @JustinEzequiel Sorry. I am dumb. I remove global code. the output is still the same. – Asher Paul Tan Jun 16 '21 at 23:50
  • I was not commenting on your code. I was commenting on where you're learning from. –  Jun 16 '21 at 23:59

1 Answers1

0

Let me start off by saying that it's great that you're asking questions, as that is part of learning. That being said, your code shows you lack fundamentals. I would recommend you go through a Python tutorial first, before jumping into bigger projects like this. It's sort of learning to drive a race car before having your driver's license.

You're welcome to ask questions here, but the community can get annoyed (as you may have noticed from the comments on your question), when you ask things that are tackled in the first chapter of Python books. So again, I'd recommend reading through a book, or going through a tutorial, and then coming back here to ask questions if you're confused.

However, I will also respond to your original question. One issue is just that you return inside the function, exiting it, before calling print. You also never actually call solve(). That's why you get no output.

A larger issue is that your Sudoku solver won't actually solve Sudokus, since it has no function to check whether you've got a valid solution.

Peatherfed
  • 178
  • 1
  • 10