Final excercise

Tried the following but keep getting “UnboundLocalError: local variable ‘p’ referenced before assignment”

however, I thought I assigned p early on. When I ask it to print p, it will give me a valid integer. Then when i try to utilize p in a subsequent function, i get an error

from robot_control_class import RobotControl
import time
robotcontrol = RobotControl()
import math

dis = robotcontrol.get_laser_full()
ang = range(0,719,1)
dictionary = dict(zip(dis, ang))
p = int(max(dis))
print (p)

def goahead():
    while p < 99999999999999999999:
            robotcontrol.rotate(int(dictionary[int(p)]) -360)
            dis = robotcontrol.getlaserfull()
            p = int(max(dis))
        print ("done")

sorry for some reason (p) gets turned into § on the forum

Hi @r.franklinismail ,

the probably reason why you have this error is that you are referencing a variable defined out of the scope.

Functions only have access to variables defined inside the function.

To access variables defined outside of the function, you first have to declare the variable as global.

Could you add “global p” in the line before “p = int(max(dis))” and also add “global p” in the first line inside the function, before using the variable, and run the script again?

Please let us know if the error persists.