难以将列表中的数字相加(不支持的操作数类型)

2024-04-26 13:04:25 发布

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

试图让这个程序为一个学校项目工作,但在代码的第26行,我得到了一个不支持的操作数类型错误'int'和'str'。任何帮助都将不胜感激。代码如下:

final = False
while final == False:

    while True:
            try:
                eventName = str(input("What is the event's name? "))
                numberJudges = int(input("How many judges are there? "))
                competitorName = str(input("What is the competitor's name? "))
                judgeScores = input("Please enter the judges scores with a space between each one. ")
                break
            except ValueError:
                print("That is not a valid name or number(s)") 
    finalJudges = numberJudges - 2

    def judgeScoreListFunction(judgeScores):
        judgeScoreList = judgeScores.split()
        return judgeScoreList

    def cleanJudgeScoresFunction(judgeScoreList):
        judgeScoreList.remove(max(judgeScoreList))
        judgeScoreList.remove(min(judgeScoreList))
        finalJudgeScores = judgeScoreList 
        return finalJudgeScores

    def cleanScoreFunction(finalJudgeScores, finalJudges):
        cleanedScore = sum(finalJudgeScores)
        finalScore = cleanedScore / finalJudges
        format(finalScore, '.2f')
        return finalScore

    judgeScoreList = judgeScoreListFunction(judgeScores)
    finalJudgeScores = cleanJudgeScoresFunction(judgeScoreList)
    finalScore = cleanScoreFunction(finalJudgeScores, finalJudges)

    if competitorName == "Finish":
        final = True

Tags: the代码nameinputreturnisdeffinal
1条回答
网友
1楼 · 发布于 2024-04-26 13:04:25

而不是:

judgeScoreList = judgeScores.split()

你想要:

judgeScoreList = [int(X) for X in judgeScores.split()]

我还建议去掉所有这些小的单用途函数。你知道吗

相关问题 更多 >