Course 2: Week 4 Final Assignment, 'UnboundLocal Error'


I keep encountering this error that says “UnboundLocalError: local variable ‘p1’ referenced before assignment” I would really appreciate some help on this

Hello @Christine_Joy_Moises,

From the error traceback I suspect that you are defining p1 in an if code block, and you are calling p1 in another if block. If I am right and the former if block doesn’t get executed because the condition isn’t satisfied, p1 would not be defined and calling it will result in the error as shown. In this case you might also need to define p1 if the condition isn’t satisfied like the following:

if ......:
    p1 = .....
    p1 = .....


Thank you very much for giving me insight into solving the problem I encountered. I found that I did not indent p1 and the if block properly in exercise 1. I have completed the course just now, many thanks again!

I see. That explains. It’s great that you debugged it yourself :wink:


1 Like