import math

def p(n):
    return 393000*((288200/393000)^n * math.exp(-(288200/393000)))/math.factorial(n)

print p(3)

运行时,会收到以下错误消息:
Traceback (most recent call last):
  File "poisson.py", line 6, in <module>
    print p(3)
  File "poisson.py", line 4, in p
    return 393000*((288200/393000)^n * math.exp(-(288200/393000)))/math.factoria
l(n)
TypeError: unsupported operand type(s) for ^: 'int' and 'float'

最佳答案

将中的^替换为**

(288200/393000)^n

Bear in mind that
288200/393000

Returns 0
也许你应该尝试使用十进制数:
import math

def p(n):
    a = 393000.0 # <-- notice the .0
    b = 288200.0
    c = b / a
    return a * ( c**n * math.exp(-c) )/ math.factorial(n)

print p(3)

Returns:
12406.890756

10-06 01:43