代码没有跳出 python 中的 while 循环

问题描述 投票:0回答:0

出于某种原因,我一直在处理的这段代码没有退出 while 循环。该代码尝试用用户输入的目标模拟用户输入的网格,并遍历边界单元格以识别最佳类型的箭头和最佳位置以在不遇到空单元格的情况下击中最多目标。

我用了一些while循环,但是当我运行代码的时候,好像是无限循环,不知道为什么。因为从理论上讲,由于“变化”变量是不断增加的,所以它应该满足打破 while 循环的 if 条件。

## FUNCTION INPUT ##

def FindArrowAI(grid_size, targets):
    
    target_list = []
    target_lis = targets.split()

    arrow_list = []
    arrow_dictionary = {}
    
    for z in target_lis:
        target_list.append(str(z))

    block_list = []

    count = 0

    size = [[0] * grid_size for _ in range(grid_size)]
    border_cells = []

    # Iterate over the cells in the size
    for i in range(grid_size):
        for j in range(grid_size):
            # Check if the current cell is on the border
            if i == 0 or i == grid_size - 1 or j == 0 or j == grid_size - 1:
                cell_coords = str(i) + str(j)  # Convert indices to strings and concatenate them
                border_cells.append(cell_coords)

    ## BLOCK IDENTIFICATION ##
    
    for block_y in range(0, grid_size, 1):
        for block_x in range(0, grid_size, 1):
            block_list.append(str(block_y) + str(block_x))
    for x in target_list:
        block_list.remove(x)


    ## TARGET IDENTIFICATION ##
    for i in target_list:
        repeat = True
        change = 1
        while repeat:
            if str(int(i[0]) + 0) + str(int(i[1]) - change) in target_list:
                loc_a = str(int(i[0]) + 0) + str(int(i[1]) + 1)
                change += 1
                count += 1
                if change == size - 1:
                    break
                else:
                    repeat = False
        if loc_a in border_cells:
            arrow_list.append(str(loc_a) + "A")
            arrow_dictionary[str(loc_a) + "A"] = count
                
    for i in target_list:
        repeat = True
        change = 1
        count = 0
        while repeat:
            if str(int(i[0]) - change) + str(int(i[1]) + 0) in target_list:
                loc_b = str(int(i[0]) + 1) + str(int(i[1]) + 0)
                change += 1
                count += 1
                if change == size - 1:
                    break
                else:
                    repeat = False
                if loc_b in border_cells:
                    arrow_list.append(str(loc_b) + "B")
                    arrow_dictionary[str(loc_b) + "B"] = count
            
    for i in target_list:
        repeat = True
        change = 1
        count = 0
        while repeat:
            if str(int(i[0]) + 0) + str(int(i[1]) + change) in target_list:
                loc_c = str(int(i[0]) + 0) + str(int(i[1]) - 1)
                change += 1
                count +=1
                if change == size - 1:
                    break
                else:
                    repeat = False
                if loc_c in border_cells:
                    arrow_list.append(str(loc_c) + "C")
                    arrow_dictionary[str(loc_c) + "C"] = count

    for i in target_list:
        repeat = True
        change = 1
        count = 0
        while repeat:
            if str(int(i[0]) + change) + str(int(i[1]) + 0) in target_list:
                loc_d = str(int(i[0]) - 1) + str(int(i[1]) + 0)
                change += 1
                count += 1
                if change == size - 1:
                    break
                else:
                    repeat = False
                if loc_d in border_cells:
                    arrow_list.append(str(loc_d) + "D")
                    arrow_dictionary[str(loc_d) + "D"] = count

    for i in target_list:
        repeat = True
        change = 1
        count = 0
        if str(int(i[0]) - change) + str(int(i[1]) - change) in target_list:
            loc_e = str(int(i[0]) + 1) + str(int(i[1]) + 1)
            change += 1
            count += 1
            if change == size - 1:
                break
            else:
                repeat = False
            if loc_e in border_cells:
                arrow_list.append(str(loc_e) + "E")
                arrow_dictionary[str(loc_e) + "E"] = count

    for i in target_list:
        repeat = True
        change = 1
        count = 0
        if str(int(i[0]) - change) + str(int(i[1]) + change) in target_list:
            loc_f = str(int(i[0]) + 1) + str(int(i[1]) -1)
            change += 1
            count += 1
            if change == size - 1:
                break
            else:
                repeat = False
            if loc_f in border_cells:
                arrow_list.append(str(loc_f) + "F")
                arrow_dictionary[str(loc_f) + "F"] = count
            
    for i in target_list:
        repeat = True
        change = 1
        count = 0
        if str(int(i[0]) + change) + str(int(i[1]) + change) in target_list:
            loc_g = str(int(i[0]) - 1) + str(int(i[1]) - 1)
            change += 1
            count += 1
            if change == size - 1:
                break
            else:
                repeat = False
            if loc_g in border_cells:
                arrow_list.append(str(loc_g) + "G")
                arrow_dictionary[str(loc_g) + "G"] = count

    for i in target_list:
        repeat = True
        change = 1
        count = 0
        if str(int(i[0]) + change) + str(int(i[1]) - change) in target_list:
            loc_h = str(int(i[0]) - 1) + str(int(i[1]) + 1)
            change += 1
            count += 1
            if change == size - 1:
                break
            else:
                repeat = False
            if loc_h in border_cells:
                arrow_list.append(str(loc_h) + "H")
                arrow_dictionary[str(loc_h) + "H"] = count
                
    print(arrow_dictionary)
  
FindArrowAI(5, "31 21 13 32 11 12")
python loops while-loop iteration break
© www.soinside.com 2019 - 2024. All rights reserved.