python - failed abstraction of variable in for loop -


I'm trying to create a 4x4 sudoku solver in Python (I'm just starting!) And defining While trying, a function to clean my code to some extent, I ran into some weird behavior that I really did not understand Obviously, there is a difference between:

  sudoku = "0200140000230040" Sudoku = map (Lambda x: '1234' if x == '0' and x, list (sudoku)) For the category I (16): For the boundary for Jammu (4): If the Sudoku [i] == str (j + 1): For the category (4): If the LAN (Sudoku [i / 4 * 4] + K])> 1: sudoku [i / 4 * 4 + k] = sudoku [i / 4 * 4 + k]. Translate (None) for category (J + 1)) (4): If LAN (Sudoku [4 * K + i% 4])> 1: sudoku [4 * k + i% 4] = sudoku [4 * k + i% 4]. Translicate (none, str (J + 1))  

and this one:

  sudoku = "0200140000230040" def sd (l): range ( 4) for: if lan (sudoku [l]) gt; 1: sudoku [L] = sudoku [L]. Translate (None, St (J + 1)) Sudoku = Map (Lambda x: '1234' if x == '0' and X, for list (Sudoku)) in range I (16): Jammu (4): If the Sudoku [i] == str (j + 1): sd (i / 4 * 4 + k) sd (4 * k + i% 4)  

rows And there are weird expressions to check the columns (boxes are not finished yet) If this kind of thing is already asked, I am very sorry to waste your time, but both code spaces See Ipet run and see the different results. thank you in advanced.

(I feel strange that I am being shouted at.)

There is a difference ... they fail with two different errors!

Firstly gives me this error:

  file "test PE", in line 9, in the & lt; Module & gt; Sudoku [i / 4 * 4 + k] = Sudoku [i / 4 * 4 + k]. Transcelet (None, String (J + 1)) Type Error: Expected a character buffer object  

The second gives me this error:

  File "test.py", line 12, & lt; Module & gt; Error on SD (I / 4 * 4 + K): Name 'K' has not been defined  

I think the main problem is that you believe that 'i / 4 * 4 + K 'will be passed as a parameter on the function, but before the function call it is actually evaluated and it fails because Kashmir is not defined You can use it instead :

  sd (lambda k: i / 4 * 4 + k)  

And inside the function, sd you can replace L with call for L (K). Receives the same error.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -