def kasSees(rida, veerg): return rida in range(3) and veerg in range(3) nihked=[[-1, 0], [0, -1], [1, 0], [0, 1]] seis="12346_758" tyhinr=seis.find("_") tyhirida=tyhinr // 3 tyhiveerg=tyhinr % 3 for nihe in nihked: if kasSees(tyhirida+nihe[0], tyhiveerg+nihe[1]): uus=list(seis) uus[tyhirida*3+tyhiveerg]=seis[(tyhirida+nihe[0])*3+tyhiveerg+nihe[1]] uus[(tyhirida+nihe[0])*3+tyhiveerg+nihe[1]]="_" print("".join(uus)) print(tyhirida, tyhiveerg)