用户输入的n个数的最小,最大和平均值

时间:2015-04-18 19:23:38

标签: python

我需要创建一个函数,用于打印用户输入的最小,最大和平均n个数字。

到目前为止,我的功能可以打印用户输入的n个数字的平均值和最大值,但我仍然坚持找到最小值。

这是我到目前为止所做的:

def main():

    n = int(input("how many?"))
    if (n>0):
        counter=0
        total=0
        l_v=0

        while counter<n:
            x = int(input("enter next number"))
            total=total+x
            counter=counter+1
            if x>l_v:
                l_v=x


        print ("the largest value is {0}".format(l_v))    
        print("the average is ", total/n)
    else:
        print("What? it is not possible to find the average, sorry ")


main()

4 个答案:

答案 0 :(得分:2)

同时跟踪最低数字,将起始值设置为float(inf)表示最小值,float(-inf)表示最大值:

if n > 0:
    l_v =  float("-inf") 
    mn_v = float("inf")
    sm, counter = 0, 0     
    while counter < n:
        x = int(input("enter next number"))
        if x < mn:
            mn = x
        if x > l_v:
            l_v = x
        sm += x
        counter += 1
    print("the largest value is {0}".format(l_v))
    print("the average is ", sm / n)
    print("the min value is {}".format(mn_v))

您还可以使用范围为的for循环:

n = int(input("how many?"))
if n > 0:
    l_v =  float("-inf")
    mn_v = float("inf")
    sm = 0
    for i in range(n):
        x = int(input("enter next number"))
        if x < mn:
            mn = x
        if x > l_v:
            l_v = x
        sm += x
    print("the largest value is {0}".format(l_v))
    print("the average is ", sm / n)
    print("the min value is {}".format(mn_v))

或使用列表comp:

n = int(input("how many?"))
if n > 0:
    nums = [int(input("enter next number")) for _ in range(n)]
    print ("the largest value is {0}".format(min(nums)))
    print("the average is ", sum(nums)/ n)
    print("the min value is {}".format(min(nums)))
else:
    print("What? it is not possible to find the average, sorry ")

sm += xaugmented assignment,与执行sm = sm + x相同。

在验证用户输入和转换时,您应该使用try/except来捕获任何exceptions

def get_num():        
    while True:
        try:
            n = int(input("enter next number"))
            # user entered valid input, return  n
            return n
        except ValueError:
            # could not be cast to int, ask again
            print("Not an interger")

while True:
    try:
        n = int(input("how many?"))
        break
    except ValueError:
        print("Not an interger")

if n > 0:
    l_v =  float("-inf") 
    mn_v = float("inf")
    sm, counter = 0, 0     
    while counter < n:
        x = int(input("enter next number"))
        if x < mn:
            mn = x
        if x > l_v:
            l_v = x
        sm += x
        counter += 1
    print("the largest value is {0}".format(l_v))
    print("the average is ", sm / n)
    print("the min value is {}".format(mn_v))

答案 1 :(得分:2)

您可以在用户输入数据时将项目添加到列表中。

用户完成后,只需在列表中使用min()max()sum()(以及len()}。

答案 2 :(得分:2)

正如@Padraic提到的那样,the answer that your posted to your question对正值不起作用,因为你的if语句只会检查min值是否小于0.

要解决此问题,您只需将第一个输入值分配给所有变量即可。这样您就不必使用正面和负面的inf变量:

def main():

    n = int(input("how many?")) 
    if (n>0):
        counter=0
        total=0
        f_v = l_v = s_v = total =int (input("enter first value"))  #first value
        while counter<(n-1):
            x = int(input("enter next number"))
            total=total+x
            counter=counter+1
            if x<s_v:    #find minimum
                s_v=x
            elif x>l_v:  #find maximum
                l_v=x

        print ("the smallest value is {0}".format(s_v))        
        print ("the largest value is {0}".format(l_v))    
        print("the average is ", total/n)
    else:
        print("What? it is not possible to find the average, sorry ")


main()

现在您的代码将运行没有任何问题:

how many?5
enter first value-1
enter next number7
enter next number0
enter next number3
enter next number2
the smallest value is -1
the largest value is 7
('the average is ', 2.2)

答案 3 :(得分:0)

这就是我最终做的事情......只是通过反复试验来解决问题。

我非常感谢答案!给我一个不同的方式来看问题!

答案:

def main():

    n = int(input("how many?"))
    if (n>0):
        counter=0
        total=1
        l_v=0
        s_v= 0
        f_v = int (input("enter first value"))
        while counter<n:
            x = int(input("enter next number"))
            total=total+x
            counter=counter+1
            if x<f_v:
                s_v=x
            elif x>l_v:
                l_v=x

        print ("the smallest value is {0}".format(s_v))        
        print ("the largest value is {0}".format(l_v))    
        print("the average is ", total/n)
    else:
        print("What? it is not possible to find the average, sorry ")


main()