following function is part of my code:
def calEntropy(p):
"""
given a list of probabilities whose sum is equal to 1,
calculate the entropy of it.
"""
try:
if 0.99<=sum(p)<=1.0:
E = sum([-x*np.log2(x) for x in p])
else:
raise ValueError("sum of probabilities must equal to 1")
except:
raise TypeError("need a list of probabilites")
return E
and when I run this function, like:
calEntropy([0.8,0.6])
I suppose it should raise the ValueError exception, but in fact, it gives TypeError, I just want to know where is wrong?
BTW, if there is a convenient way to check the value and type of each element in the list? or I have to write a loop to check them?