Python将用户的中位数添加到奇数用户输入

时间:2018-10-29 20:19:24

标签: python input median

我正在尝试获取给定薪水的用户中位数。将其实现为当前代码是否容易?

 def Lista():

    values = []
    print("Give your salary from smallest number to largerst.")
    while True:
        sum1 = int(input("Give your salary (0 ends): "))

        if sum1 == 0:
            print("Salary average:",sum(values)/len(values))
            print("Salary minimum:",min(values))
            print("Salary maximum:",max(values))
            print("Salary median:",values[len(values)/2])
            return False

        else:
            values.append(sum1)

    return Lista

Lista()

我自己似乎无法做到。 我只会用奇数个要求的薪水

1 个答案:

答案 0 :(得分:0)

这是一种获取整数值和长度的方法;

int(values[int(len(values)/2)])

平均而言,我还注意到它是唯一以xxxx.0结尾的代码,可以这样编辑,使其与其他代码相同;

int(sum(values)/len(values))

所以完整的代码看起来像这样

def Lista():

    values = []
    print("Give your salary from smallest number to largerst.")
    while True:
        sum1 = int(input("Give your salary (0 ends): "))

        if sum1 == 0:
            print("Salary average:",int(sum(values)/len(values)))
            print("Salary minimum:",min(values))
            print("Salary maximum:",max(values))
            print("Salary median:",int(values[int(len(values)/2)]))
            return False

        else:
            values.append(sum1)

    return Lista

Lista()

希望这会有所帮助:) E:总和重叠,将其更改为sum1