“ while”循环过早退出(python 3)

时间:2018-08-18 11:21:03

标签: python

while循环仅运行一次,就好像已经满足了条件一样退出,但是,在调试是否在外部(在循环之后)满足条件时进行调试。

这是我的代码:

iFile = open("snakein.txt")
line = iFile.readline()
finXStr,finYStr = line.split()

finX = int(finXStr)
finY = int(finYStr)



moves = []

snkX = 0
snkY = 0

while snkX != finX and snkY != finY:
    if finX > 0 and finX != snkX:
        moves.append("R")
        snkX += 1
        print("x moves")
    elif finX < 0 and finX != snkX:
        moves.append("L")
        snkX -= 1
        print("x moves")
    elif finX == snkX:
        moves.append("L")
        snkX -= 1
        print("x moves")
    if finY < 0 and moves[-1] == "R":
        moves.append("R")
        snkY -= 1
        print("y moves")
    elif finY < 0 and moves[-1] == "L":
        moves.append("L")
        snkY -= 1
        print("y moves")
    elif finY > 0 and moves[-1] == "R":
        moves.append("L")
        snkY += 1
        print("y moves")
    elif finY > 0 and moves[-1] == "L":
        moves.append("R")
        snkY += 1
        print("y moves")
    elif finY == snkY:
        moves.appebd("L")
        snakeY -= 1
        print("y moves")

output = moves

oFile = open("snakeout.txt", "w")
oFile.write(str(output))

程序正在尝试通过某些动作,在诸如“设置”之类的“笛卡尔平面”上,以最有效的方式将“蛇”动作移动到特定目标。

1 个答案:

答案 0 :(得分:0)

在您的情况下,看来while snkX != finX or snkY != finY:可能更有意义...