Error in the testing code of last assignment of week 4

i have run the test and the printing are exact like the test printing but i am getting some weired error

Hi @Zvi_Boiangou ,

Where is p1 created? Python doesn’t know what p1 is and hence reporting the error.

it is created in the proc below . the test of this proc passed OK.

def compute_entropy(y):

# You need to return the following variables correctly
entropy = 0.

### START CODE HERE ###

if len(y) != 0:
 # Your code here to calculate the fraction of edible examples (i.e with value = 1 in y)
    p1 = len(y[y == 1]) / len(y) 

    # For p1 = 0 and 1, set the entropy to 0 (to handle 0log0)
if p1 != 0 and p1 != 1:
     # Your code here to calculate the entropy using the formula provided above
    entropy = -p1 * np.log2(p1) - (1 - p1) * np.log2(1 - p1)
else:
    entropy = 0       
       
### END CODE HERE ###        
print(entropy)
return entropy

And what happens to your p1 = statement if len(y) equals zero?

Passing the unit tests does not mean your code is perfect.

Hi @Zvi_Boiangou ,

The problem is with the indentation. When len(y) ==0, the second if statement is executed, here p1 has not been created because your code said p1 is only created when len(y) !=0.
The second if and else statement should be within the first if statement block.

OK , i got you .
it should have been
if bla
if blabla

thanks

i mean - 2 chaining if statements .
from some reason the tab is not reflected .