Course 2 Week4 Assignment Grading Issue

Hi. I am getting following errors after the grading of my assignment. Although my codes are running fine and outputs are also coinciding with the expected outputs.

Cell #11. Can’t compile the student’s code. Error: UnboundLocalError(“local variable ‘p1’ referenced before assignment”)
Traceback (most recent call last):
File “/home/www/app/grading/”, line 112, in handle_solution_errors
yield {}
File “/home/www/app/grading/”, line 393, in _grade
context =
File “/home/www/app/grading/submission/”, line 195, in run
return list(self._code_items.values())[cell_num - 1].run()
File “/home/www/app/grading/submission/”, line 54, in run
return import_module(self.import_statement, items)
File “/usr/local/lib/python3.7/importlib/”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1006, in _gcd_import
File “”, line 983, in _find_and_load
File “”, line 967, in _find_and_load_unlocked
File “”, line 677, in _load_unlocked
File “”, line 728, in exec_module
File “”, line 219, in _call_with_frames_removed
File “/tmp/student_solution_cells/”, line 23, in
File “/tmp/”, line 105, in compute_information_gain_test
result = target(X, y, node_indexes, 1)
File “/tmp/student_solution_cells/”, line 42, in compute_information_gain
left_entropy = compute_entropy(y_left)
File “/tmp/student_solution_cells/”, line 30, in compute_entropy
if p1 != 0 and p1 != 1:
UnboundLocalError: local variable ‘p1’ referenced before assignment

How can I resolve this issue?


There is an error in your compute entropy function. It does not handle the case where length of y is zero.

I have modified the code for that inclusion but still no improvement in the grade.

A lot of learners have posted about this same issue before. Let me explain the UnboundLocalError error in more detail.

Consider the following code snippet:

def t():
    if m == n:
        a = 5
    if a >= 1:

The first if statement checks whether the values of m and n are equal. If they are not equal, then the variable a is never assigned the value of 5. This means that when the second if statement attempts to reference a, the variable has not yet been initialized, which raises an UnboundLocalError.

To fix the error (in the context of the variable p1 ), you need to place:

:point_up_2:this block of code precisely where the variable p1 is declared.

Similar to the below example :
def t():
    if m == n:
        a = 5
        if a >= 1:

This will ensure that when both the values of m and n are equal, you assign and then reference the variable a within that single if block.

No. I am still confused. I guess my code is logically correct. P1 is an assigned variable with an expression inside the first IF statement containing len(y) condition and defined before if p1 != 0 and p1 != 1:

Please ensure that this 'if p1 != 0 and p1 != 1:' block of code should be nested within the first if statement where the condition len(y) is being checked.

Thank you so much. The issue has been sorted out.