'Taking the updated variable at the end of a for loop to be used in the same for loop in python

I'm new to coding and having some trouble working on a sudoku solver. I made a for loop that goes through every blank position in the sudoku and finds the possible numbers for each empty space. If there's only a possible answer for that space, the sudoku is updated putting that number in that specific position. When the loop is finished it gives me an updated version of the sudoku in which the blanks that had only 1 possible answer after filling the blanks before them have been filled.

Here's an example of what it gets in every loop. Each line stands for each of the 81 spaces in the sudoku and contains the possible numbers for that specific space in a list and the coordenates (X-number of line-number of column-number of 9x9 square in which the sudoku is divided).

['1', '2', '4', '5'] X0-0-0
['1', '4'] X0-3-1
['1', '2', '4', '7'] X0-4-1
['4', '7'] X0-5-1
['1', '7'] X0-6-2
['7', '9'] X0-7-2
['1', '2', '3', '4'] X1-0-0
['2', '3', '4'] X1-1-0     
['1', '2'] X1-2-0
['1', '2', '4', '7'] X1-4-1
['1', '7'] X1-8-2
['1'] X2-0-0
['2', '3', '5'] X3-0-3
['2', '5', '9'] X3-2-3
['4', '8'] X3-5-4
['3'] X3-6-5
['4', '5', '9'] X3-7-5
['5', '8', '9'] X3-8-5
['3', '8'] X4-1-3
['5'] X4-3-4
['8'] X4-5-4
['6'] X4-6-5

In the example I showed, the code would keep going through the blanks until it arrives to the blank in the third line, first column, where it will update the sudoku putting the number 1 in that position. The next loops will be made with the updated sudoku.

The sudoku I get at the end of the for-loop still has many blanks, so it has to go through the loop again. I have copied and pasted the for-loop one after another and if I paste it enough times, the sudoku gets solved, but I want to know if there is a better wat to do that.

This is the for-loop:

num2 = -1
for num in solución:
    pos2 += 1
    #Si el número es una incógnita
    if num[0] == 'X':
        posibles = []
        #Metemos las posibles soluciones a cada incógnita en una lista
        for i in range(1,10):
            if str(i) not in sol_filas[int(num[1])] and str(i) not in sol_columnas[int(num[3])] and str(i) not in sol_cuadros[pos_cuadro(int(num[1]),int(num[3]))]:
                posibles.append(str(i)) 
        print(posibles,num)   
        # Si la lista de posibles soluciones solo tiene un elemento es porque solo hay una opción
        if len(posibles) == 1:
            solución[pos2] = posibles[0] #Actualizamos la lista con el valor correcto
            sol_filas[int(num[1])][int(num[3])] = posibles[0]
            sol_columnas[int(num[3])][int(num[1])] = posibles[0]
            sol_cuadros[pos_cuadro(int(num[1]),int(num[3]))][3*((int(num[1])%3))+((int(num[3])%3))] = posibles[0]
        print(solución)
print(solución)


Solution 1:[1]

You'd need to wrap it in a while loop to keep trying values, then exit when either you find a solution or if you can't find get any improvements, e.g.

while True:
    improved = False
    for num in soluciĆ³n:
        .... #your code
        # when you fill in a number set improved to True
    if improved = False: 
    #this means we can't do any better the loop will go forever
        break # we leave the loop

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 monk