+ 2
Please help me in Python.
I'm writing a code for this sudoku challenge: https://www.sololearn.com/learn/16856/?ref=app This is my code: https://code.sololearn.com/cK4WkTyt4xmm/?ref=app But it keeps printing True even if the sudoku is wrong.
4 Respuestas
+ 4
The bug is in the two lines where you declare `cnc = 0` and `cnr = 0` just after the if statement. If I'm not wrong, then I think you'd meant to set it to zero after the innermost loop gets over!
So, just move them after the `for i in range(9):` , like below:
https://code.sololearn.com/cw2rooNia7qd/#py
+ 2
rv7 Thank you
+ 1
rv7 Thank you so much
It worked
But please can you tell me why by just changing the position of cnr and cnc made such difference. I thought that I'm was just reassigning cnr and cnc, so that would have not mattered that much.
+ 1
Samad, in python indentation is the real magic.
When you wrote it like this:
---------------------------------
cnr += 1
if cnr >= 2:
return False
cnr = 0
---------------------------------
then, it assigned cnr a zero value "just after" it passed the if statement. So, it that way, the if condition will always evaluate to True as cnr will never be greater than 1.
But, when `cnr = 0` moves out of innermost for loop, then it is supposed to evaluate after loop gets over. Therfore, the two if statement became meaningful.
And, same is for `cnc = 0`