python - 编程新手并试图完成学校的这项任务,我似乎无法将列表中的数字添加在一起

标签 python

我有一些代码,用于我们必须在学校完成的任务。我正在尝试在列表中添加一些数字,以便我可以计算该数字的平均值。每当我尝试这样做时,它只会告诉我它是 int 和 str 不受支持的操作数类型。我认为我必须做的是将输入转换为 float 而不是字符串,但我不知道该怎么做。代码如下:

final = False
while final == False: 
    judgeScoreForLoop = 0
    judgeScoreLoop = []

    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? "))
            for judgeScoreForLoop in range (0, numberJudges):
                judgeScore = input("Enter the judge score here: ")
                judgeScoreLoop.append(judgeScore)
                judgeScoreForLoop + 1
            break
        except ValueError:
            print("One of the inputs was invalid, please try again.")

    finalJudges = numberJudges - 2

    judgeScoreCombined = sum(judgeScoreLoop)
    judgeFinalScore = judgeScoreCombined / finalJudges

    if competitorName == "Finish".lower():
        final = True

    print(judgeFinalScore)

最佳答案

您可以将输入直接转换为 float

  judgeScore = float(input("Enter the judge score here: "))

但最好先检查用户是否提供了号码 prehaps 使用 isdigit() 我不想透露太多,但这应该有帮助

关于python - 编程新手并试图完成学校的这项任务,我似乎无法将列表中的数字添加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758812/

相关文章:

python:如何在一副牌中生成成对的牌

python - 在python中导入类

python - 如何将 html 列表添加到 dash 布局

Python,将列表的所有条目从字符串转换为 float

python - 有没有办法导出/导入 Jupyter Notebook 扩展?

python - Pandas 扩展日期时间索引,查找上一年

python - 如何使用 cv2 保存二进制图像(dtype=bool)?

python - 在 Python Paramiko 中的 SSH 服务器上的辅助 shell/命令中执行(子)命令

python - 将字符串数字列表转换为 float 字列表

python - 如何在pyfiglet python3中获取所有可用的字体