|
I don't quite get why the code I submitted was wrong. It works with the sample sudokus mentioned in the exercise, and it worked with the test cases I came up with. Feedback is appreciated! Thank you! Here's the code:
=============== You can check the results yourself with either of those two commands: |
|
I think it doesn't run when it's called from the grader since you have If I try to run the code multiple times, since Edit: Putting LOL I was mesmerized by the duplication, didn't even notice the lists at the top o.O And I completely missed the duplication! Thanks for the feedback. I didn't know how the grader actually works and assumed the procedure gets just called once and then restarted (at least that's how I tested my code in an online Python shell). Well, you live and learn. I think that what happened is also something that could happen if you make the code to fit in with someone else's code so it's a good thing to find out about now when it's just a course rather than if you were coding something important later in life and the same sort of bug arose because the variables weren't set to zero for each example run through the code. |
|
I think you have your first example on the perils of global variables. After you run check_sudoku on an incorrect sudoku, since then, the following calls to check_sudoku will return False though it is correct or not. For example:
You get:
Ups, the second time you call check_sudoku(correct) you get False. Why? Because of the global variable row that you did not reset to [] before append new elements. Your row variable contains all the elements from previous function calls. My humble suggestion: Try to avoid global variables. You could have those variables as local variables inside each method instead. |
Thanks for sharing your code, it's a nice variation on what I've seen before.