I am messing around with the lambda function and I understand what I can do with it in a simple fashion, but when I try something more advanced I am running into errors and I don’t see why.
Here is what I am trying if you can tell me where I am going wrong it would be appricated.
import math
C = lambda n,k: math.factorial(n)/(math.factorial(k))(math.factorial(n-k))
print C(10,5)
I should note that I am running into errors trying to run the code on Codepad. I do not have access to Idle.
Try this:
You were missing a
*, and also it’s possible that the division should take in consideration decimals, so the old division operator/won’t do. That’s why I’m importing the new/operator, which performs decimal division.UPDATE:
Well, after all it seems like it’s Codepad’s fault – it supports Python 2.5.1, and
factorialwas added in Python 2.6. Just implement your own factorial function and be done with it, or even better start using a real Python interpreter.