-1

I have the following code for a maze solving bot:

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]

The problem is the axioms variable. I have it outside of the calculateNext function and when its values change, within calculateNext such as,

    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()

they do not save to the variable and the same values are used instead of the new ones.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

0

To maintain the value of a variable across functions you have several options, such as: use a global variable, use a class to share properties along methods, use a mutable variable and mutate rather than reassign it, or pass the variable as a parameter and return it at the end of the function.

As axioms is a dict and therefore mutable, rewriting rotate to change its values in place (rather than reassigning) might work best for you

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

You would then just call it as rotate()

Stuart
  • 9,597
  • 1
  • 21
  • 30