python - How to not add the same element in a list -
i need again...
here code :
def import_sudoku(): open("sudoku.txt") f: lines = f.read() sudoku = [[character character in line if not character == " "] line in lines.split("\n")] return sudoku sudoku = import_sudoku() print(sudoku) def grid_index(grid, value): i, row in enumerate(grid): j, cell in enumerate(row): if cell == value: return i, j return -1, -1 print("coords:",grid_index(sudoku, ".")) def solve_next_unsolved(sudoku): coords = grid_index(sudoku, ".") value_to_input = "3" cell in sudoku[0]: if value_to_input == cell: break else: sudoku[coords[0]][coords[0]] = value_to_input print(sudoku)
my sudoku.txt...
.3. ... ... ..8 39. 6.. 5.1 2.. 49. .7. 6.. ... 2.. ... .4. ... 5.3 98. ... ... 15. ... ..7 ..9 4.. .1. 3..
so, working first line @ moment. don't understand why 3 can stored in line... in fact, when break while when 3 found in first line, it's not supposed break while , end loop... ??
last thing : if see profil, third post in same object... (sudoku solver...) possible keep topic problem , not create topic every time have problem... ?
if wants me not here (and if have time ... etc), skype : lakyrorr
really sorry inconvenience.
your else belongs wrong block: indentation makes belong loop , not if statement intended:
for cell in sudoku[0]: if value_to_input == cell: break else: sudoku[coords[0]][coords[0]] = value_to_input
instead of looping on list elements can check
if value_to_input in row: print "duplicate entry" break
to respond general question: way stackoverflow works deal small individual problems. snot forum discuss larger programming projects whewre same people @ code again time time.
Comments
Post a Comment