输入列表的中间项

时间:2018-05-03 14:40:20

标签: python

我需要取3个数字并打印最大,最小和中期。这是我写的:

new_list = []
for i in range(3):
    new_list.append(int(input("Enter a number: ")))

x = min(new_list)
y = max(new_list)
z = 


print("The max is %d, the middle is %d and the min is é %d." % (y, x , z))

但我不知道如何定义z所以它可以是中期。有什么帮助吗?

2 个答案:

答案 0 :(得分:3)

只需对它们进行排序:

x, z, y = sorted(new_list)

答案 1 :(得分:2)

您也可以使用列表理解来完成。

new_list = []
for i in range(3):
    new_list.append(int(input("Enter a number: ")))

x = min(new_list)
y = max(new_list)
z = [i for i in new_list if i!=x and i!=y][0]

print("The max is %d, the middle is %d and the min is é %d." % (y, z , x))