繁体   English   中英

在Python中找到平均值

[英]Find average in 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

如何找到奇数的平均值?

您可以做两件事:

  • 将所有这些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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM