从python列表中选择最高分数

2024-05-13 05:26:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经开始了一个代码,我做不同回合的舞蹈比赛,必须消除最差的两对夫妇每轮。你知道吗

我目前在第二轮,我让用户输入他/她自己的结果作为评委,但无法从用户输入的分数列表中删除最差的两对。你知道吗

到目前为止,我的情况是:

cA2_judge1 = int(input("score couple A out of 10"))
cA2_judge2 = int(input("score couple A out of 10"))
cA2_judge3 = int(input("score couple A out of 10"))
cA2_judge4 = int(input("score couple A out of 10"))
cA2_judge5 = int(input("score couple A out of 10"))
print("")
cC2_judge1 = int(input("score couple C out of 10"))
cC2_judge2 = int(input("score couple C out of 10"))
cC2_judge3 = int(input("score couple C out of 10"))
cC2_judge4 = int(input("score couple C out of 10"))
cC2_judge5 = int(input("score couple C out of 10"))
print("")
cD2_judge1 = int(input("score couple D out of 10"))
cD2_judge2 = int(input("score couple D out of 10"))
cD2_judge3 = int(input("score couple D out of 10"))
cD2_judge4 = int(input("score couple D out of 10"))
cD2_judge5 = int(input("score couple D out of 10"))
print("")
cF2_judge1 = int(input("score couple F out of 10"))
cF2_judge2 = int(input("score couple F out of 10"))
cF2_judge3 = int(input("score couple F out of 10"))
cF2_judge4 = int(input("score couple F out of 10"))
cF2_judge5 = int(input("score couple F out of 10"))


listA2 = [cA2_judge1, cA2_judge2, cA2_judge3, cA2_judge4, 
cA2_judge5]
listA2.remove(min(listA2))
listA2.remove(max(listA2))
scoresA2=listA2
print("-------------------------")
print(".                       .")
print("Couple A scored",scoresA2)
print("This makes their total", sum(scoresA2))

listC2 = [cC2_judge1, cC2_judge2, cC2_judge3, cC2_judge4, 
cC2_judge5]
listC2.remove(min(listC2))
listC2.remove(max(listC2))
scoresC2=listC2
print("-------------------------")
print(".                       .")
print("Couple C scored",scoresC2)
print("This makes their total", sum(scoresC2))

listD2 = [cD2_judge1, cD2_judge2, cD2_judge3, cD2_judge4, 
cD2_judge5]
listD2.remove(min(listD2))
listD2.remove(max(listD2))
scoresD2=listD2
print("-------------------------")
print(".                       .")
print("Couple D scored",scoresD2)
print("This makes their total", sum(scoresD2))

listF2 = [cF2_judge1, cF2_judge2, cF2_judge3, cF2_judge4, 
cF2_judge5]
listF2.remove(min(listF2))
listF2.remove(max(listF2))
scoresF2=listF2
print("-------------------------")
print(".                       .")
print("Couple F scored",scoresF2)
print("This makes their total", sum(scoresF2))

listR2 = [sum(scoresA2), sum(scoresC2), sum(scoresD2), 
sum(scoresF2)]
listR2.remove(min(listR2))
listR2.remove(min(listR2))
print("")
print("")
print("This leaves us with the highest score of",max(listR2))

Tags: ofinputoutremoveintscoreprintcf2
1条回答
网友
1楼 · 发布于 2024-05-13 05:26:40

您的代码确实消除了2个最差的分数,但是丢失了对夫妇的引用。您应该使用一个成对的列表(couple, score),根据分数对其排序,然后删除最低的2对。这样你就可以显示出夫妻之间还剩下什么了。你知道吗

您可以使用:

listR2 = [('A', sum(scoresA2)), ('C', sum(scoresC2)), ('D', sum(scoresD2)), 
('F', sum(scoresF2))]
listR2 = sorted(listR2, key = lambda x: x[1], reverse = True)[:-2]
print("Remaining couples are",
    " and ".join(("{0} with a total score of {1}".format(*i) for i in listR2)))

相关问题 更多 >