Nenad Savic
Nenad Savic

Reputation: 77

Find average in python

can anyone help?

import math
a = int(raw_input('Enter Average:')) # Average number probability
c = int(raw_input('Enter first number:')) #
d = int(raw_input('Enter last number:')) #
e = 2.71828
for b in range(c,d+1):
    x = (a**b)/math.factorial(b)*(e**-a)
    odd = round (1/x*0.92, 2)
    print odd

How to find average value of odd?

Upvotes: 0

Views: 147

Answers (1)

abarnert
abarnert

Reputation: 365617

You can do two things:

  • Accumulate all of those odd values into, e.g., a list, then average the result.
  • Keep a running total and count and divide.

(I'm assuming that by "average" you mean "arithmetic mean". If you mean something different, the details are different, but the basic idea is the same.)

For the first:

odds = []
for b in range(c,d+1):
    x = (a**b)/math.factorial(b)*(e**-a)
    odd = round (1/x*0.92, 2)
    print odd
    odds.append(odd)
print sum(odds) / len(odds)

If you don't what sum or len do, read the docs.

For the second:

total, count = 0, 0
for b in range(c,d+1):
    x = (a**b)/math.factorial(b)*(e**-a)
    odd = round (1/x*0.92, 2)
    print odd
    total += odd
    count += 1
print total / count

Upvotes: 2

Related Questions