0

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?

2
  • 2
    It would be nice if you could explain what your program was trying to do, what problem you're facing, and how we could help. Often doing that helps you solve your own query. Also, as much as we'd like, we can't read minds. Commented Sep 12, 2014 at 23:08
  • 2
    Your question is very unclear. What exactly is your goal? Average of what? Commented Sep 12, 2014 at 23:08

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

that's it, I forgot append to use, thank you so much :)
And for the completely unreadable one-(and a half)-liner: a=int(raw_input("probability: ")); result=sum([round(1/((a**b)/math.factorial(b)*(2.71828**(-a)))*0.92, 2) for count,b in enumerate(range(int(raw_input("start: ")), int(raw_input("end: "))+1),1)])/count
(assuming you're in python2, of course, since iirc py3 wouldn't leak the count variable outside of the list comp)
@AdamSmith: You're right, 3.x, won't leak the count… but you don't need it thanks to statistics. Which means you don't need enumerate either. Plus input is shorter than raw_input, so I can waste even more of my comment writing text and still fit the 3.4 code with plenty of room to spare: a=int(input('probability:')); result=statistics.mean(round(1/((a**b)/math.factorial(b)*(2.71828**-a))*0.92, 2) for b in range(int(input('start:')),int(input('end:'))+1)). That's 169 characters vs. 202, proving that 3.x is 19.5% better than 2.x. :)
@abarnert can't argue with statistics (or statistics!)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.