Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix this python program?

Tags:

python

import math

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

print p(3)

When I run it, I get the following error message:

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'
like image 839
neuromancer Avatar asked Nov 24 '25 14:11

neuromancer


2 Answers

Replace ^ with ** in

(288200/393000)^n

Bear in mind that

288200/393000

Returns 0

Maybe you should try using decimal numbers:

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
like image 86
OscarRyz Avatar answered Nov 26 '25 02:11

OscarRyz


Is the ^ supposed to mean exponentiation? If so, use ** instead.

like image 30
Fred Larson Avatar answered Nov 26 '25 02:11

Fred Larson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!