在python中找到平均值

时间:2014-09-12 23:04:46

标签: python

任何人都可以帮忙吗?

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

如何找到奇数的平均值?

1 个答案:

答案 0 :(得分:2)

你可以做两件事:

  • 将所有这些odd值累积到例如list中,然后对结果取平均值。
  • 保持一个总计,并计算和分割。

(我假设通过"平均"你的意思是"算术意味着"如果你的意思不同,细节是不同的,但基本的想法是相同的。)

第一个:

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)

如果您没有sumlen做什么,请阅读文档。

第二个:

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