如果使用';而';使用此代码在python中循环?

如果使用';而';使用此代码在python中循环?,python,while-loop,average,mean,Python,While Loop,Average,Mean,使用此代码在python中使用“while”循环时,如何找到平均值 n1 = int (input("Enter a value up 100, enter 0 when finished")) high =n1 while n1!=0: if n1 > high: high=n1 n1 = int(input("Enter another number")) 本节不断告诉用户输入值,从这里开始我不知道如何计算平均值。只需跟踪总和和总计数,然后您可以随

使用此代码在python中使用“while”循环时,如何找到平均值

 n1 = int (input("Enter a value up 100, enter 0 when finished"))
 high =n1

while n1!=0:
     if n1 > high:
     high=n1
     n1 = int(input("Enter another number"))

本节不断告诉用户输入值,从这里开始我不知道如何计算平均值。

只需跟踪
总和和
总计数,然后您可以随时计算平均值

n1 = int (input("Enter a value up 100, enter 0 when finished"))
high = n1
total_count = 0
sum = 0

while n1 != 0:
    count += 1
    sum += n1
    if n1 > high:
        high=n1
    n1 = int(input("Enter another number"))

如果在while循环中添加一个计数器变量,即:

while n1 != 0:
    loopCounter += 1
您将获得输入的数字量,并且您还需要输入的所有数字的总数,如您所知。您可以在循环中执行类似的操作:

while n1 != 0:
    total += n1
sum = total / loopCounter
print("Your average is",sum)

你试过什么?你大概知道如何计算纸上的平均值?我知道如何计算平均值,我尝试了sum=n1/(我不知道在这里添加什么),这就是我感到困惑的地方,然后打印(“这是你的平均值”,sum)如果你想在添加所有数字时保持最新,有几种方法可以做到这一点。这将有助于保持输入的数字之和,而不是迄今为止看到的最大值。