价值之外的变量不会永久改变

时间:2020-02-14 22:39:31

标签: python python-3.x

我有以下迷宫求解机器人代码:

def main():
    axioms = {
        "Y":"F",
        "X":"R",
        "-Y":"B",
        "-X":"L"
        }

    def rotate():
        axioms = list(axioms.items())
        new = []
        new.append((axioms[0][0], axioms[1][1]))
        new.append((axioms[1][0], axioms[2][1]))
        new.append((axioms[2][0], axioms[3][1]))
        new.append((axioms[3][0], axioms[0][1]))
        return dict(new)

    def move(dir):
        if dir == "F":
            log("Moving forward.")
            API.moveForward()
        elif dir == "L":
            log("Turning left.")
            axioms = rotate()
            API.turnLeft()
            API.moveForward()
        elif dir == "R":
            log("Turning right.")
            axioms = rotate()
            axioms = rotate()
            axioms = rotate()
            API.turnRight()
            API.moveForward()
        elif dir == "B":
            log("Turning back.")
            axioms = rotate()
            axioms = rotate()
            API.turnLeft()
            API.turnLeft()
            API.moveForward()

    def calculateNext(current):
        #calculate possible turns
        if current[3] == False:
            if sensor.front() == True:
                del current[2][(next(key for key, value in axioms.items() if value == "F"))]
            if sensor.left() == True:
                del current[2][(next(key for key, value in axioms.items() if value == "L"))]
            if sensor.right() == True:
                del current[2][(next(key for key, value in axioms.items() if value == "R"))]
            current[3] = True

        #find preferred turn
        values = []
        for temp in current[2]:
            values.append(current[2][temp][1])

        #check for inconsistency
        def retry():
            global preferred
            if all(x==values[0] for x in values) == True and values[0] > current[1]:
                current[1] += 2
                retry()
            else:
                preferred = min(values)
        retry()

        preferedTurns = []
        #for temp in current[2]:
        preferedTurns.append(next(key for key, value in current[2].items() if value[1] == preferred))

        return preferedTurns[0]

问题是axioms变量。我将其放置在calculateNext函数之外,并且当它的值更改时,它在calculateNext之内,例如

    def rotate():
        axioms = list(axioms.items())
        new = []
        new.append((axioms[0][0], axioms[1][1]))
        new.append((axioms[1][0], axioms[2][1]))
        new.append((axioms[2][0], axioms[3][1]))
        new.append((axioms[3][0], axioms[0][1]))
        return dict(new)
...
...
...

    def move(dir):
        if dir == "F":
            log("Moving forward.")
            API.moveForward()
        elif dir == "L":
            log("Turning left.")
            axioms = rotate(axioms)
            API.turnLeft()
            API.moveForward()
        elif dir == "R":
            log("Turning right.")
            axioms = rotate(axioms)
            axioms = rotate(axioms)
            axioms = rotate(axioms)
            API.turnRight()
            API.moveForward()
        elif dir == "B":
            log("Turning back.")
            axioms = rotate(axioms)
            axioms = rotate(axioms)
            API.turnLeft()
            API.turnLeft()
            API.moveForward()

它们不保存到变量中,而是使用相同的值代替新的值。

1 个答案:

答案 0 :(得分:0)

要在各个函数之间维护变量的值,您可以使用几种选择,例如:使用全局变量,使用类沿方法共享属性,使用可变变量并进行变异而不是重新分配,或将变量作为参数,并在函数末尾将其返回。

由于公理是字典,因此是易变的,因此重写rotate来更改其值(而不是重新分配)可能对您来说最合适

def rotate():
    values = list(axioms.values())
    for k, v in zip(axioms, values[1:] + [values[0]]):
        axioms[k] = v

然后您将其称为rotate()

相关问题