import math
def p(n):
return 393000*((288200/393000)^n * math.exp(-(288200/393000)))/math.factorial(n)
print p(3)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到以下错误消息:
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'
Run Code Online (Sandbox Code Playgroud)
替换^为**in
(288200/393000)^n
Run Code Online (Sandbox Code Playgroud)
请记住这一点
288200/393000
Run Code Online (Sandbox Code Playgroud)
返回 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)
Run Code Online (Sandbox Code Playgroud)
返回:
12406.890756
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
366 次 |
| 最近记录: |