Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question

put on hold as off-topic by Simon André Forsberg, Manny Meng, Jamal 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Simon André Forsberg, Manny Meng, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information. –  Simon André Forsberg 2 days ago

Browse other questions tagged or ask your own question.