0

I've written code for a sudoku-solver and chase the bugs right now. There is a function "insert" that should copy the dictionary "mglwerte" and change it. But the function changes not only the copy but the original dictionary, despite using copy at the beginning of the function. Here is the call of the function:

    print(ebene, ": 748: ", mglwerte["748"], " / 779: ", mglwerte["779"])
    sudokuneu, mglwerteneu, geklappt = insert(int(var[0]), int(var[1]), wert, sudoku, mglwerte)
    print(ebene, ": 748: ", mglwerte["748"], " / 779: ", mglwerte["779"])

And the function itself:

def insert(z,s,v,sudokualt,mglwertealt):
    sudoku = sudokualt.copy()
    mglwerte = mglwertealt.copy()
    geschafft = True
    q = 3*int((z-1)/3) + int((s-1)/3) + 1
    keyfull = str(z) + str(s) + str(q)

    if v not in mglwerte[keyfull]:
        print(v, "not in ", keyfull)
        return None, None, False

    sudoku[keyfull] = v
    mglwerte[keyfull] = {}    

    for key,value in mglwerte.items():
        if len(value) > 0:
            if str(key[0]) == str(z) or str(key[1]) == str(s) or str(key[2]) == str(q):
                value.discard(v)
                mglwerte[key] = value
                if len(value) < 1:
                    geschafft = False
    return sudoku, mglwerte, geschafft

The print-statements in the first piece of code show me, that "mglwerte" is changed after the call of the function. How can I change this?

MrData
  • 1

0 Answers0